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