Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs @ 18242

Last change on this file since 18242 was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 7.2 KB
Line 
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.
32using System;
33using System.Reflection;
34
35namespace 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        {
66            // Convert the reflection call into an open delegate, i.e. instead of calling x.Method()
67            // we'll call getter(x).
68            Func<TSource, TResult> getter =
69                (Func<TSource, TResult>) Delegate.CreateDelegate(typeof (Func<TSource, TResult>), null, method);
70
71            // Implicit upcast to object (within the delegate)
72            return delegate(TSource source) { return getter(source); };
73        }
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
86        public static Action<TSource, object> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method)
87        {
88            // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
89            // call Method(x, y)
90            Action<TSource, TParam> call =
91                (Action<TSource, TParam>) Delegate.CreateDelegate(typeof (Action<TSource, TParam>), null, method);
92
93            return delegate(TSource source, object parameter) { call(source, (TParam) parameter); };
94        }
95
96        /// <summary>
97        /// Creates a delegate which will execute the given method after casting the parameter
98        /// down from object to the required parameter type.
99        /// </summary>
100        public static Action<T, object> CreateDowncastDelegateIgnoringReturn<T>(MethodInfo method)
101        {
102            MethodInfo openImpl = typeof (ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl");
103            MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof (T), method.GetParameters()[0].ParameterType,
104                                                               method.ReturnType);
105            return (Action<T, object>) closedImpl.Invoke(null, new object[] {method});
106        }
107
108        public static Action<TSource, object> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(
109            MethodInfo method)
110        {
111            // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
112            // call Method(x, y)
113            Func<TSource, TParam, TReturn> call = (Func<TSource, TParam, TReturn>)
114                                                  Delegate.CreateDelegate(typeof (Func<TSource, TParam, TReturn>), null,
115                                                                          method);
116
117            return delegate(TSource source, object parameter) { call(source, (TParam) parameter); };
118        }
119
120        /// <summary>
121        /// Creates a delegate which will execute the given static method and cast the result up to IBuilder.
122        /// </summary>
123        public static Func<IBuilder> CreateStaticUpcastDelegate(MethodInfo method)
124        {
125            MethodInfo openImpl = typeof (ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl");
126            MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType);
127            return (Func<IBuilder>) closedImpl.Invoke(null, new object[] {method});
128        }
129
130        public static Func<IBuilder> CreateStaticUpcastDelegateImpl<T>(MethodInfo method)
131        {
132            Func<T> call = (Func<T>) Delegate.CreateDelegate(typeof (Func<T>), null, method);
133            return delegate { return (IBuilder) call(); };
134        }
135    }
136}
Note: See TracBrowser for help on using the repository browser.