ReflectionUtil.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://github.com/jskeet/dotnet-protobufs/
  4. // Original C++/Java/Python code:
  5. // http://code.google.com/p/protobuf/
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. using System;
  33. using System.Reflection;
  34. namespace Google.ProtocolBuffers.FieldAccess
  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 execute the given method and then return
  52. /// the result as an object.
  53. /// </summary>
  54. public static Func<T, object> CreateUpcastDelegate<T>(MethodInfo method)
  55. {
  56. // The tricky bit is invoking CreateCreateUpcastDelegateImpl with the right type parameters
  57. MethodInfo openImpl = typeof (ReflectionUtil).GetMethod("CreateUpcastDelegateImpl");
  58. MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof (T), method.ReturnType);
  59. return (Func<T, object>) closedImpl.Invoke(null, new object[] {method});
  60. }
  61. /// <summary>
  62. /// Method used solely for implementing CreateUpcastDelegate. Public to avoid trust issues
  63. /// in low-trust scenarios, e.g. Silverlight.
  64. /// TODO(jonskeet): Check any of this actually works in Silverlight...
  65. /// </summary>
  66. public static Func<TSource, object> CreateUpcastDelegateImpl<TSource, TResult>(MethodInfo method)
  67. {
  68. // Convert the reflection call into an open delegate, i.e. instead of calling x.Method()
  69. // we'll call getter(x).
  70. Func<TSource, TResult> getter =
  71. (Func<TSource, TResult>) Delegate.CreateDelegate(typeof (Func<TSource, TResult>), null, method);
  72. // Implicit upcast to object (within the delegate)
  73. return delegate(TSource source) { return getter(source); };
  74. }
  75. /// <summary>
  76. /// Creates a delegate which will execute the given method after casting the parameter
  77. /// down from object to the required parameter type.
  78. /// </summary>
  79. public static Action<T, object> CreateDowncastDelegate<T>(MethodInfo method)
  80. {
  81. MethodInfo openImpl = typeof (ReflectionUtil).GetMethod("CreateDowncastDelegateImpl");
  82. MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof (T), method.GetParameters()[0].ParameterType);
  83. return (Action<T, object>) closedImpl.Invoke(null, new object[] {method});
  84. }
  85. public static Action<TSource, object> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method)
  86. {
  87. // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
  88. // call Method(x, y)
  89. Action<TSource, TParam> call =
  90. (Action<TSource, TParam>) Delegate.CreateDelegate(typeof (Action<TSource, TParam>), null, method);
  91. return delegate(TSource source, object parameter) { call(source, (TParam) parameter); };
  92. }
  93. /// <summary>
  94. /// Creates a delegate which will execute the given method after casting the parameter
  95. /// down from object to the required parameter type.
  96. /// </summary>
  97. public static Action<T, object> CreateDowncastDelegateIgnoringReturn<T>(MethodInfo method)
  98. {
  99. MethodInfo openImpl = typeof (ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl");
  100. MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof (T), method.GetParameters()[0].ParameterType,
  101. method.ReturnType);
  102. return (Action<T, object>) closedImpl.Invoke(null, new object[] {method});
  103. }
  104. public static Action<TSource, object> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(
  105. MethodInfo method)
  106. {
  107. // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
  108. // call Method(x, y)
  109. Func<TSource, TParam, TReturn> call = (Func<TSource, TParam, TReturn>)
  110. Delegate.CreateDelegate(typeof (Func<TSource, TParam, TReturn>), null,
  111. method);
  112. return delegate(TSource source, object parameter) { call(source, (TParam) parameter); };
  113. }
  114. /// <summary>
  115. /// Creates a delegate which will execute the given static method and cast the result up to IBuilder.
  116. /// </summary>
  117. public static Func<IBuilder> CreateStaticUpcastDelegate(MethodInfo method)
  118. {
  119. MethodInfo openImpl = typeof (ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl");
  120. MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType);
  121. return (Func<IBuilder>) closedImpl.Invoke(null, new object[] {method});
  122. }
  123. public static Func<IBuilder> CreateStaticUpcastDelegateImpl<T>(MethodInfo method)
  124. {
  125. Func<T> call = (Func<T>) Delegate.CreateDelegate(typeof (Func<T>), null, method);
  126. return delegate { return (IBuilder) call(); };
  127. }
  128. }
  129. }