Commit b01135c4 authored by Jon Skeet's avatar Jon Skeet

Align delegates with .NET 3.5

parent 6d0cbe72
...@@ -16,13 +16,11 @@ ...@@ -16,13 +16,11 @@
namespace Google.ProtocolBuffers.FieldAccess { namespace Google.ProtocolBuffers.FieldAccess {
// TODO(jonskeet): Convert these to Func/Action family // These delegate declarations mirror the ones in .NET 3.5 for the sake of familiarity.
delegate bool HasDelegate<T>(T message); internal delegate TResult Func<TResult>();
delegate T ClearDelegate<T>(T builder); internal delegate TResult Func<T, TResult>(T arg);
delegate int RepeatedCountDelegate<T>(T message); internal delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
delegate object GetValueDelegate<T>(T message); internal delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
delegate void SingleValueDelegate<TSource>(TSource source, object value); internal delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
delegate IBuilder CreateBuilderDelegate(); internal delegate void Action<T1, T2>(T1 arg1, T2 arg2);
delegate object GetIndexedValueDelegate<T>(T message, int index);
delegate object SetIndexedValueDelegate<T>(T message, int index, object value);
} }
...@@ -32,25 +32,23 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -32,25 +32,23 @@ namespace Google.ProtocolBuffers.FieldAccess {
/// Creates a delegate which will execute the given method and then return /// Creates a delegate which will execute the given method and then return
/// the result as an object. /// the result as an object.
/// </summary> /// </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 // The tricky bit is invoking CreateCreateUpcastDelegateImpl with the right type parameters
MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateUpcastDelegateImpl"); MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateUpcastDelegateImpl");
MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.ReturnType); 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> /// <summary>
/// Method used solely for implementing CreateUpcastDelegate. Public to avoid trust issues /// Method used solely for implementing CreateUpcastDelegate. Public to avoid trust issues
/// in low-trust scenarios, e.g. Silverlight. /// in low-trust scenarios, e.g. Silverlight.
/// TODO(jonskeet): Check any of this actually works in Silverlight... /// TODO(jonskeet): Check any of this actually works in Silverlight...
/// </summary> /// </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() // Convert the reflection call into an open delegate, i.e. instead of calling x.Method()
// we'll call getter(x). // 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) // Implicit upcast to object (within the delegate)
return delegate(TSource source) { return getter(source); }; return delegate(TSource source) { return getter(source); };
...@@ -61,19 +59,16 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -61,19 +59,16 @@ namespace Google.ProtocolBuffers.FieldAccess {
/// Creates a delegate which will execute the given method after casting the parameter /// Creates a delegate which will execute the given method after casting the parameter
/// down from object to the required parameter type. /// down from object to the required parameter type.
/// </summary> /// </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 openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateImpl");
MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType); 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 Action<TSource, object> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method) {
public static SingleValueDelegate<TSource> CreateDowncastDelegateImpl<TSource, TParam>(MethodInfo method) {
// Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
// call Method(x, y) // call Method(x, y)
OpenSingleValueDelegate<TSource, TParam> call = (OpenSingleValueDelegate<TSource, TParam>) Action<TSource, TParam> call = (Action<TSource, TParam>) Delegate.CreateDelegate(typeof(Action<TSource, TParam>), method);
Delegate.CreateDelegate(typeof(OpenSingleValueDelegate<TSource, TParam>), method);
return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; return delegate(TSource source, object parameter) { call(source, (TParam)parameter); };
} }
...@@ -82,36 +77,32 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -82,36 +77,32 @@ namespace Google.ProtocolBuffers.FieldAccess {
/// Creates a delegate which will execute the given method after casting the parameter /// Creates a delegate which will execute the given method after casting the parameter
/// down from object to the required parameter type. /// down from object to the required parameter type.
/// </summary> /// </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 openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl");
MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType, method.ReturnType); 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 Action<TSource, object> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(MethodInfo method) {
public static SingleValueDelegate<TSource> CreateDowncastDelegateIgnoringReturnImpl<TSource, TParam, TReturn>(MethodInfo method) {
// Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll
// call Method(x, y) // call Method(x, y)
OpenSingleValueDelegate<TSource, TParam, TReturn> call = (OpenSingleValueDelegate<TSource, TParam, TReturn>) Func<TSource, TParam, TReturn> call = (Func<TSource, TParam, TReturn>)
Delegate.CreateDelegate(typeof(OpenSingleValueDelegate<TSource, TParam, TReturn>), method); Delegate.CreateDelegate(typeof(Func<TSource, TParam, TReturn>), method);
return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; return delegate(TSource source, object parameter) { call(source, (TParam)parameter); };
} }
delegate T OpenCreateBuilderDelegate<T>();
/// <summary> /// <summary>
/// Creates a delegate which will execute the given static method and cast the result up to IBuilder. /// Creates a delegate which will execute the given static method and cast the result up to IBuilder.
/// </summary> /// </summary>
public static CreateBuilderDelegate CreateStaticUpcastDelegate(MethodInfo method) { public static Func<IBuilder> CreateStaticUpcastDelegate(MethodInfo method) {
MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl"); MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateStaticUpcastDelegateImpl");
MethodInfo closedImpl = openImpl.MakeGenericMethod(method.ReturnType); 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) { public static Func<IBuilder> CreateStaticUpcastDelegateImpl<T>(MethodInfo method) {
OpenCreateBuilderDelegate<T> call = (OpenCreateBuilderDelegate<T>)Delegate.CreateDelegate(typeof(OpenCreateBuilderDelegate<T>), method); Func<T> call = (Func<T>)Delegate.CreateDelegate(typeof(Func<T>), method);
return delegate { return (IBuilder)call(); }; return delegate { return (IBuilder)call(); };
} }
} }
......
...@@ -33,7 +33,7 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -33,7 +33,7 @@ namespace Google.ProtocolBuffers.FieldAccess {
/// in a message type "Foo", a field called "bar" might be of type "Baz". This /// in a message type "Foo", a field called "bar" might be of type "Baz". This
/// method is Baz.CreateBuilder. /// method is Baz.CreateBuilder.
/// </summary> /// </summary>
private readonly CreateBuilderDelegate createBuilderDelegate; private readonly Func<IBuilder> createBuilderDelegate;
internal RepeatedMessageAccessor(string name) : base(name) { internal RepeatedMessageAccessor(string name) : base(name) {
MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]); MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]);
......
...@@ -26,11 +26,11 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -26,11 +26,11 @@ namespace Google.ProtocolBuffers.FieldAccess {
where TBuilder : IBuilder<TMessage, TBuilder> { where TBuilder : IBuilder<TMessage, TBuilder> {
private readonly Type clrType; private readonly Type clrType;
private readonly GetValueDelegate<TMessage> getValueDelegate; private readonly Func<TMessage, object> getValueDelegate;
private readonly ClearDelegate<TBuilder> clearDelegate; private readonly Func<TBuilder, IBuilder> clearDelegate;
private readonly SingleValueDelegate<TBuilder> addValueDelegate; private readonly Action<TBuilder, object> addValueDelegate;
private readonly GetValueDelegate<TBuilder> getRepeatedWrapperDelegate; private readonly Func<TBuilder, object> getRepeatedWrapperDelegate;
private readonly RepeatedCountDelegate<TMessage> countDelegate; private readonly Func<TMessage, int> countDelegate;
private readonly MethodInfo getElementMethod; private readonly MethodInfo getElementMethod;
private readonly MethodInfo setElementMethod; private readonly MethodInfo setElementMethod;
...@@ -62,9 +62,9 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -62,9 +62,9 @@ namespace Google.ProtocolBuffers.FieldAccess {
|| setElementMethod == null) { || setElementMethod == null) {
throw new ArgumentException("Not all required properties/methods available"); throw new ArgumentException("Not all required properties/methods available");
} }
clearDelegate = (ClearDelegate<TBuilder>)Delegate.CreateDelegate(typeof(ClearDelegate<TBuilder>), clearMethod); clearDelegate = (Func<TBuilder, IBuilder>)Delegate.CreateDelegate(typeof(Func<TBuilder, IBuilder>), clearMethod);
countDelegate = (RepeatedCountDelegate<TMessage>)Delegate.CreateDelegate countDelegate = (Func<TMessage, int>)Delegate.CreateDelegate
(typeof(RepeatedCountDelegate<TMessage>), countProperty.GetGetMethod()); (typeof(Func<TMessage, int>), countProperty.GetGetMethod());
getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod()); getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn<TBuilder>(addMethod); addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn<TBuilder>(addMethod);
getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate<TBuilder>(builderProperty.GetGetMethod()); getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate<TBuilder>(builderProperty.GetGetMethod());
......
...@@ -29,7 +29,7 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -29,7 +29,7 @@ namespace Google.ProtocolBuffers.FieldAccess {
/// in a message type "Foo", a field called "bar" might be of type "Baz". This /// in a message type "Foo", a field called "bar" might be of type "Baz". This
/// method is Baz.CreateBuilder. /// method is Baz.CreateBuilder.
/// </summary> /// </summary>
private readonly CreateBuilderDelegate createBuilderDelegate; private readonly Func<IBuilder> createBuilderDelegate;
internal SingleMessageAccessor(string name) : base(name) { internal SingleMessageAccessor(string name) : base(name) {
MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]); MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]);
......
...@@ -25,10 +25,11 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -25,10 +25,11 @@ namespace Google.ProtocolBuffers.FieldAccess {
where TBuilder : IBuilder<TMessage, TBuilder> { where TBuilder : IBuilder<TMessage, TBuilder> {
private readonly Type clrType; private readonly Type clrType;
private readonly GetValueDelegate<TMessage> getValueDelegate; private readonly Func<TMessage, object> getValueDelegate;
private readonly SingleValueDelegate<TBuilder> setValueDelegate; private readonly Action<TBuilder, object> setValueDelegate;
private readonly HasDelegate<TMessage> hasDelegate; private readonly Func<TMessage, bool> hasDelegate;
private readonly ClearDelegate<TBuilder> clearDelegate; private readonly Func<TBuilder, IBuilder> clearDelegate;
delegate void SingleValueDelegate<TSource>(TSource source, object value);
/// <summary> /// <summary>
/// The CLR type of the field (int, the enum type, ByteString, the message etc). /// The CLR type of the field (int, the enum type, ByteString, the message etc).
...@@ -47,8 +48,8 @@ namespace Google.ProtocolBuffers.FieldAccess { ...@@ -47,8 +48,8 @@ namespace Google.ProtocolBuffers.FieldAccess {
throw new ArgumentException("Not all required properties/methods available"); throw new ArgumentException("Not all required properties/methods available");
} }
clrType = messageProperty.PropertyType; clrType = messageProperty.PropertyType;
hasDelegate = (HasDelegate<TMessage>)Delegate.CreateDelegate(typeof(HasDelegate<TMessage>), hasProperty.GetGetMethod()); hasDelegate = (Func<TMessage, bool>)Delegate.CreateDelegate(typeof(Func<TMessage, bool>), hasProperty.GetGetMethod());
clearDelegate = (ClearDelegate<TBuilder>)Delegate.CreateDelegate(typeof(ClearDelegate<TBuilder>), clearMethod); clearDelegate = (Func<TBuilder, IBuilder>)Delegate.CreateDelegate(typeof(Func<TBuilder, IBuilder>), clearMethod);
getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod()); getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod()); setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment