Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 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 | |
| 19 | namespace Google.ProtocolBuffers.FieldAccess { |
| 20 | |
| 21 | /// <summary> |
| 22 | /// The methods in this class are somewhat evil, and should not be tampered with lightly. |
| 23 | /// Basically they allow the creation of relatively weakly typed delegates from MethodInfos |
| 24 | /// which are more strongly typed. They do this by creating an appropriate strongly typed |
| 25 | /// delegate from the MethodInfo, and then calling that within an anonymous method. |
| 26 | /// Mind-bending stuff (at least to your humble narrator) but the resulting delegates are |
| 27 | /// very fast compared with calling Invoke later on. |
| 28 | /// </summary> |
| 29 | internal static class ReflectionUtil { |
| 30 | |
| 31 | /// <summary> |
| 32 | /// Creates a delegate which will execute the given method and then return |
| 33 | /// the result as an object. |
| 34 | /// </summary> |
| 35 | public static Func<T, object> CreateUpcastDelegate<T>(MethodInfo method) { |
| 36 | |
| 37 | // The tricky bit is invoking CreateCreateUpcastDelegateImpl with the right type parameters |
| 38 | MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateUpcastDelegateImpl"); |
| 39 | MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.ReturnType); |
| 40 | return (Func<T, object>) closedImpl.Invoke(null, new object[] { method }); |
| 41 | } |
| 42 | |
| 43 | /// <summary> |
| 44 | /// Method used solely for implementing CreateUpcastDelegate. Public to avoid trust issues |
| 45 | /// in low-trust scenarios, e.g. Silverlight. |
| 46 | /// TODO(jonskeet): Check any of this actually works in Silverlight... |
| 47 | /// </summary> |
| 48 | public static Func<TSource, object> CreateUpcastDelegateImpl<TSource, TResult>(MethodInfo method) { |
| 49 | // Convert the reflection call into an open delegate, i.e. instead of calling x.Method() |
| 50 | // we'll call getter(x). |
| 51 | Func<TSource, TResult> getter = (Func<TSource, TResult>)Delegate.CreateDelegate(typeof(Func<TSource, TResult>), method); |
| 52 | |
| 53 | // Implicit upcast to object (within the delegate) |
| 54 | return delegate(TSource source) { return getter(source); }; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /// <summary> |
| 59 | /// Creates a delegate which will execute the given method after casting the parameter |
| 60 | /// down from object to the required parameter type. |
| 61 | /// </summary> |
| 62 | public static Action<T, object> CreateDowncastDelegate<T>(MethodInfo method) { |
| 63 | MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateImpl"); |
| 64 | MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType); |
| 65 | return (Action<T, object>) closedImpl.Invoke(null, new object[] { method }); |
| 66 | } |
| 67 | |
| 68 | public static Action<TSource, object> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method) { |
| 69 | // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll |
| 70 | // call Method(x, y) |
| 71 | Action<TSource, TParam> call = (Action<TSource, TParam>) Delegate.CreateDelegate(typeof(Action<TSource, TParam>), method); |
| 72 | |
| 73 | return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; |
| 74 | } |
| 75 | |
| 76 | /// <summary> |
| 77 | /// Creates a delegate which will execute the given method after casting the parameter |
| 78 | /// down from object to the required parameter type. |
| 79 | /// </summary> |
| 80 | public static Action<T, object> CreateDowncastDelegateIgnoringReturn<T>(MethodInfo method) { |
| 81 | MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl"); |
| 82 | MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType, method.ReturnType); |
| 83 | return (Action<T, object>)closedImpl.Invoke(null, new object[] { method }); |
| 84 | } |
| 85 | |
| 86 | public static Action<TSource, object> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(MethodInfo method) { |
| 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 | Func<TSource, TParam, TReturn> call = (Func<TSource, TParam, TReturn>) |
| 90 | Delegate.CreateDelegate(typeof(Func<TSource, TParam, TReturn>), method); |
| 91 | |
| 92 | return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; |
| 93 | } |
| 94 | |
| 95 | /// <summary> |
| 96 | /// Creates a delegate which will execute the given static method and cast the result up to IBuilder. |
| 97 | /// </summary> |
| 98 | public static Func<IBuilder> CreateStaticUpcastDelegate(MethodInfo method) { |
| 99 | MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl"); |
| 100 | MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType); |
| 101 | return (Func<IBuilder>)closedImpl.Invoke(null, new object[] { method }); |
| 102 | } |
| 103 | |
| 104 | public static Func<IBuilder> CreateStaticUpcastDelegateImpl<T>(MethodInfo method) { |
| 105 | Func<T> call = (Func<T>)Delegate.CreateDelegate(typeof(Func<T>), method); |
| 106 | return delegate { return (IBuilder)call(); }; |
| 107 | } |
| 108 | } |
| 109 | } |