|
|
|
@ -32,25 +32,23 @@ namespace Google.ProtocolBuffers.FieldAccess { |
|
|
|
|
/// Creates a delegate which will execute the given method and then return |
|
|
|
|
/// the result as an object. |
|
|
|
|
/// </summary> |
|
|
|
|
public static GetValueDelegate<T> CreateUpcastDelegate<T>(MethodInfo method) { |
|
|
|
|
public static Func<T, object> CreateUpcastDelegate<T>(MethodInfo method) { |
|
|
|
|
|
|
|
|
|
// The tricky bit is invoking CreateCreateUpcastDelegateImpl with the right type parameters |
|
|
|
|
MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateUpcastDelegateImpl"); |
|
|
|
|
MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.ReturnType); |
|
|
|
|
return (GetValueDelegate<T>) closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
return (Func<T, object>) closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delegate TResult Getter<TSource, TResult>(TSource source); |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Method used solely for implementing CreateUpcastDelegate. Public to avoid trust issues |
|
|
|
|
/// in low-trust scenarios, e.g. Silverlight. |
|
|
|
|
/// TODO(jonskeet): Check any of this actually works in Silverlight... |
|
|
|
|
/// </summary> |
|
|
|
|
public static GetValueDelegate<TSource> CreateUpcastDelegateImpl<TSource, TResult>(MethodInfo method) { |
|
|
|
|
public static Func<TSource, object> CreateUpcastDelegateImpl<TSource, TResult>(MethodInfo method) { |
|
|
|
|
// Convert the reflection call into an open delegate, i.e. instead of calling x.Method() |
|
|
|
|
// we'll call getter(x). |
|
|
|
|
Getter<TSource, TResult> getter = (Getter<TSource, TResult>)Delegate.CreateDelegate(typeof(Getter<TSource, TResult>), method); |
|
|
|
|
Func<TSource, TResult> getter = (Func<TSource, TResult>)Delegate.CreateDelegate(typeof(Func<TSource, TResult>), method); |
|
|
|
|
|
|
|
|
|
// Implicit upcast to object (within the delegate) |
|
|
|
|
return delegate(TSource source) { return getter(source); }; |
|
|
|
@ -61,19 +59,16 @@ namespace Google.ProtocolBuffers.FieldAccess { |
|
|
|
|
/// Creates a delegate which will execute the given method after casting the parameter |
|
|
|
|
/// down from object to the required parameter type. |
|
|
|
|
/// </summary> |
|
|
|
|
public static SingleValueDelegate<T> CreateDowncastDelegate<T>(MethodInfo method) { |
|
|
|
|
public static Action<T, object> CreateDowncastDelegate<T>(MethodInfo method) { |
|
|
|
|
MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateImpl"); |
|
|
|
|
MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType); |
|
|
|
|
return (SingleValueDelegate<T>)closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
return (Action<T, object>) closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delegate void OpenSingleValueDelegate<TSource, TParam>(TSource source, TParam parameter); |
|
|
|
|
|
|
|
|
|
public static SingleValueDelegate<TSource> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method) { |
|
|
|
|
public static Action<TSource, object> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method) { |
|
|
|
|
// Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll |
|
|
|
|
// call Method(x, y) |
|
|
|
|
OpenSingleValueDelegate<TSource, TParam> call = (OpenSingleValueDelegate<TSource, TParam>) |
|
|
|
|
Delegate.CreateDelegate(typeof(OpenSingleValueDelegate<TSource, TParam>), method); |
|
|
|
|
Action<TSource, TParam> call = (Action<TSource, TParam>) Delegate.CreateDelegate(typeof(Action<TSource, TParam>), method); |
|
|
|
|
|
|
|
|
|
return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; |
|
|
|
|
} |
|
|
|
@ -82,36 +77,32 @@ namespace Google.ProtocolBuffers.FieldAccess { |
|
|
|
|
/// Creates a delegate which will execute the given method after casting the parameter |
|
|
|
|
/// down from object to the required parameter type. |
|
|
|
|
/// </summary> |
|
|
|
|
public static SingleValueDelegate<T> CreateDowncastDelegateIgnoringReturn<T>(MethodInfo method) { |
|
|
|
|
public static Action<T, object> CreateDowncastDelegateIgnoringReturn<T>(MethodInfo method) { |
|
|
|
|
MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl"); |
|
|
|
|
MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType, method.ReturnType); |
|
|
|
|
return (SingleValueDelegate<T>)closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
return (Action<T, object>)closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delegate TReturn OpenSingleValueDelegate<TSource, TParam, TReturn>(TSource source, TParam parameter); |
|
|
|
|
|
|
|
|
|
public static SingleValueDelegate<TSource> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(MethodInfo method) { |
|
|
|
|
public static Action<TSource, object> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(MethodInfo method) { |
|
|
|
|
// Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll |
|
|
|
|
// call Method(x, y) |
|
|
|
|
OpenSingleValueDelegate<TSource, TParam, TReturn> call = (OpenSingleValueDelegate<TSource, TParam, TReturn>) |
|
|
|
|
Delegate.CreateDelegate(typeof(OpenSingleValueDelegate<TSource, TParam, TReturn>), method); |
|
|
|
|
Func<TSource, TParam, TReturn> call = (Func<TSource, TParam, TReturn>) |
|
|
|
|
Delegate.CreateDelegate(typeof(Func<TSource, TParam, TReturn>), method); |
|
|
|
|
|
|
|
|
|
return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delegate T OpenCreateBuilderDelegate<T>(); |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Creates a delegate which will execute the given static method and cast the result up to IBuilder. |
|
|
|
|
/// </summary> |
|
|
|
|
public static CreateBuilderDelegate CreateStaticUpcastDelegate(MethodInfo method) { |
|
|
|
|
public static Func<IBuilder> CreateStaticUpcastDelegate(MethodInfo method) { |
|
|
|
|
MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl"); |
|
|
|
|
MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType); |
|
|
|
|
return (CreateBuilderDelegate) closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
return (Func<IBuilder>)closedImpl.Invoke(null, new object[] { method }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static CreateBuilderDelegate CreateStaticUpcastDelegateImpl<T>(MethodInfo method) { |
|
|
|
|
OpenCreateBuilderDelegate<T> call = (OpenCreateBuilderDelegate<T>)Delegate.CreateDelegate(typeof(OpenCreateBuilderDelegate<T>), method); |
|
|
|
|
public static Func<IBuilder> CreateStaticUpcastDelegateImpl<T>(MethodInfo method) { |
|
|
|
|
Func<T> call = (Func<T>)Delegate.CreateDelegate(typeof(Func<T>), method); |
|
|
|
|
return delegate { return (IBuilder)call(); }; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|