ReflectionUtil.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Reflection;
  34. namespace Google.Protobuf.Reflection
  35. {
  36. /// <summary>
  37. /// The methods in this class are somewhat evil, and should not be tampered with lightly.
  38. /// Basically they allow the creation of relatively weakly typed delegates from MethodInfos
  39. /// which are more strongly typed. They do this by creating an appropriate strongly typed
  40. /// delegate from the MethodInfo, and then calling that within an anonymous method.
  41. /// Mind-bending stuff (at least to your humble narrator) but the resulting delegates are
  42. /// very fast compared with calling Invoke later on.
  43. /// </summary>
  44. internal static class ReflectionUtil
  45. {
  46. /// <summary>
  47. /// Empty Type[] used when calling GetProperty to force property instead of indexer fetching.
  48. /// </summary>
  49. internal static readonly Type[] EmptyTypes = new Type[0];
  50. /// <summary>
  51. /// Creates a delegate which will cast the argument to the appropriate method target type,
  52. /// call the method on it, then convert the result to object.
  53. /// </summary>
  54. internal static Func<IMessage, object> CreateFuncIMessageObject(MethodInfo method) =>
  55. GetReflectionHelper(method.DeclaringType, method.ReturnType).CreateFuncIMessageObject(method);
  56. /// <summary>
  57. /// Creates a delegate which will cast the argument to the appropriate method target type,
  58. /// call the method on it, then convert the result to the specified type.
  59. /// </summary>
  60. internal static Func<IMessage, int> CreateFuncIMessageInt32(MethodInfo method) =>
  61. GetReflectionHelper(method.DeclaringType, typeof(object)).CreateFuncIMessageInt32(method);
  62. /// <summary>
  63. /// Creates a delegate which will execute the given method after casting the first argument to
  64. /// the target type of the method, and the second argument to the first parameter type of the method.
  65. /// </summary>
  66. internal static Action<IMessage, object> CreateActionIMessageObject(MethodInfo method) =>
  67. GetReflectionHelper(method.DeclaringType, method.GetParameters()[0].ParameterType).CreateActionIMessageObject(method);
  68. /// <summary>
  69. /// Creates a delegate which will execute the given method after casting the first argument to
  70. /// the target type of the method.
  71. /// </summary>
  72. internal static Action<IMessage> CreateActionIMessage(MethodInfo method) =>
  73. GetReflectionHelper(method.DeclaringType, typeof(object)).CreateActionIMessage(method);
  74. /// <summary>
  75. /// Creates a reflection helper for the given type arguments. Currently these are created on demand
  76. /// rather than cached; this will be "busy" when initially loading a message's descriptor, but after that
  77. /// they can be garbage collected. We could cache them by type if that proves to be important, but creating
  78. /// an object is pretty cheap.
  79. /// </summary>
  80. private static IReflectionHelper GetReflectionHelper(Type t1, Type t2) =>
  81. (IReflectionHelper) Activator.CreateInstance(typeof(ReflectionHelper<,>).MakeGenericType(t1, t2));
  82. // Non-generic interface allowing us to use an instance of ReflectionHelper<T1, T2> without statically
  83. // knowing the types involved.
  84. private interface IReflectionHelper
  85. {
  86. Func<IMessage, int> CreateFuncIMessageInt32(MethodInfo method);
  87. Action<IMessage> CreateActionIMessage(MethodInfo method);
  88. Func<IMessage, object> CreateFuncIMessageObject(MethodInfo method);
  89. Action<IMessage, object> CreateActionIMessageObject(MethodInfo method);
  90. }
  91. private class ReflectionHelper<T1, T2> : IReflectionHelper
  92. {
  93. public Func<IMessage, int> CreateFuncIMessageInt32(MethodInfo method)
  94. {
  95. var del = (Func<T1, int>) method.CreateDelegate(typeof(Func<T1, int>));
  96. return message => del((T1) message);
  97. }
  98. public Action<IMessage> CreateActionIMessage(MethodInfo method)
  99. {
  100. var del = (Action<T1>) method.CreateDelegate(typeof(Action<T1>));
  101. return message => del((T1) message);
  102. }
  103. public Func<IMessage, object> CreateFuncIMessageObject(MethodInfo method)
  104. {
  105. var del = (Func<T1, T2>) method.CreateDelegate(typeof(Func<T1, T2>));
  106. return message => del((T1) message);
  107. }
  108. public Action<IMessage, object> CreateActionIMessageObject(MethodInfo method)
  109. {
  110. var del = (Action<T1, T2>) method.CreateDelegate(typeof(Action<T1, T2>));
  111. return (message, arg) => del((T1) message, (T2) arg);
  112. }
  113. }
  114. }
  115. }