ReflectionUtil.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Reflection;
  18. namespace Google.ProtocolBuffers.FieldAccess {
  19. /// <summary>
  20. /// The methods in this class are somewhat evil, and should not be tampered with lightly.
  21. /// Basically they allow the creation of relatively weakly typed delegates from MethodInfos
  22. /// which are more strongly typed. They do this by creating an appropriate strongly typed
  23. /// delegate from the MethodInfo, and then calling that within an anonymous method.
  24. /// Mind-bending stuff (at least to your humble narrator) but the resulting delegates are
  25. /// very fast compared with calling Invoke later on.
  26. /// </summary>
  27. internal static class ReflectionUtil {
  28. /// <summary>
  29. /// Creates a delegate which will execute the given method and then return
  30. /// the result as an object.
  31. /// </summary>
  32. public static Func<T, object> CreateUpcastDelegate<T>(MethodInfo method) {
  33. // The tricky bit is invoking CreateCreateUpcastDelegateImpl with the right type parameters
  34. MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateUpcastDelegateImpl");
  35. MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.ReturnType);
  36. return (Func<T, object>) closedImpl.Invoke(null, new object[] { method });
  37. }
  38. /// <summary>
  39. /// Method used solely for implementing CreateUpcastDelegate. Public to avoid trust issues
  40. /// in low-trust scenarios, e.g. Silverlight.
  41. /// TODO(jonskeet): Check any of this actually works in Silverlight...
  42. /// </summary>
  43. public static Func<TSource, object> CreateUpcastDelegateImpl<TSource, TResult>(MethodInfo method) {
  44. // Convert the reflection call into an open delegate, i.e. instead of calling x.Method()
  45. // we'll call getter(x).
  46. Func<TSource, TResult> getter = (Func<TSource, TResult>)Delegate.CreateDelegate(typeof(Func<TSource, TResult>), method);
  47. // Implicit upcast to object (within the delegate)
  48. return delegate(TSource source) { return getter(source); };
  49. }
  50. /// <summary>
  51. /// Creates a delegate which will execute the given method after casting the parameter
  52. /// down from object to the required parameter type.
  53. /// </summary>
  54. public static Action<T, object> CreateDowncastDelegate<T>(MethodInfo method) {
  55. MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateImpl");
  56. MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType);
  57. return (Action<T, object>) closedImpl.Invoke(null, new object[] { method });
  58. }
  59. public static Action<TSource, object> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method) {
  60. // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
  61. // call Method(x, y)
  62. Action<TSource, TParam> call = (Action<TSource, TParam>) Delegate.CreateDelegate(typeof(Action<TSource, TParam>), method);
  63. return delegate(TSource source, object parameter) { call(source, (TParam)parameter); };
  64. }
  65. /// <summary>
  66. /// Creates a delegate which will execute the given method after casting the parameter
  67. /// down from object to the required parameter type.
  68. /// </summary>
  69. public static Action<T, object> CreateDowncastDelegateIgnoringReturn<T>(MethodInfo method) {
  70. MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl");
  71. MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType, method.ReturnType);
  72. return (Action<T, object>)closedImpl.Invoke(null, new object[] { method });
  73. }
  74. public static Action<TSource, object> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(MethodInfo method) {
  75. // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
  76. // call Method(x, y)
  77. Func<TSource, TParam, TReturn> call = (Func<TSource, TParam, TReturn>)
  78. Delegate.CreateDelegate(typeof(Func<TSource, TParam, TReturn>), method);
  79. return delegate(TSource source, object parameter) { call(source, (TParam)parameter); };
  80. }
  81. /// <summary>
  82. /// Creates a delegate which will execute the given static method and cast the result up to IBuilder.
  83. /// </summary>
  84. public static Func<IBuilder> CreateStaticUpcastDelegate(MethodInfo method) {
  85. MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl");
  86. MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType);
  87. return (Func<IBuilder>)closedImpl.Invoke(null, new object[] { method });
  88. }
  89. public static Func<IBuilder> CreateStaticUpcastDelegateImpl<T>(MethodInfo method) {
  90. Func<T> call = (Func<T>)Delegate.CreateDelegate(typeof(Func<T>), method);
  91. return delegate { return (IBuilder)call(); };
  92. }
  93. }
  94. }