From b01135c485bf6ae179924b2024450d4692e9dc38 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 14 Aug 2008 20:38:08 +0100 Subject: [PATCH] Align delegates with .NET 3.5 --- .../ProtocolBuffers/FieldAccess/Delegates.cs | 16 +++---- .../FieldAccess/ReflectionUtil.cs | 43 ++++++++----------- .../FieldAccess/RepeatedMessageAccessor.cs | 2 +- .../FieldAccess/RepeatedPrimitiveAccessor.cs | 16 +++---- .../FieldAccess/SingleMessageAccessor.cs | 2 +- .../FieldAccess/SinglePrimitiveAccessor.cs | 13 +++--- 6 files changed, 41 insertions(+), 51 deletions(-) diff --git a/csharp/ProtocolBuffers/FieldAccess/Delegates.cs b/csharp/ProtocolBuffers/FieldAccess/Delegates.cs index e6fb4fe8..972961c6 100644 --- a/csharp/ProtocolBuffers/FieldAccess/Delegates.cs +++ b/csharp/ProtocolBuffers/FieldAccess/Delegates.cs @@ -16,13 +16,11 @@ namespace Google.ProtocolBuffers.FieldAccess { - // TODO(jonskeet): Convert these to Func/Action family - delegate bool HasDelegate(T message); - delegate T ClearDelegate(T builder); - delegate int RepeatedCountDelegate(T message); - delegate object GetValueDelegate(T message); - delegate void SingleValueDelegate(TSource source, object value); - delegate IBuilder CreateBuilderDelegate(); - delegate object GetIndexedValueDelegate(T message, int index); - delegate object SetIndexedValueDelegate(T message, int index, object value); + // These delegate declarations mirror the ones in .NET 3.5 for the sake of familiarity. + internal delegate TResult Func(); + internal delegate TResult Func(T arg); + internal delegate TResult Func(T1 arg1, T2 arg2); + internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3); + internal delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4); + internal delegate void Action(T1 arg1, T2 arg2); } diff --git a/csharp/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/csharp/ProtocolBuffers/FieldAccess/ReflectionUtil.cs index ca768a76..ddec2229 100644 --- a/csharp/ProtocolBuffers/FieldAccess/ReflectionUtil.cs +++ b/csharp/ProtocolBuffers/FieldAccess/ReflectionUtil.cs @@ -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. /// - public static GetValueDelegate CreateUpcastDelegate(MethodInfo method) { + public static Func CreateUpcastDelegate(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) closedImpl.Invoke(null, new object[] { method }); + return (Func) closedImpl.Invoke(null, new object[] { method }); } - delegate TResult Getter(TSource source); - /// /// 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... /// - public static GetValueDelegate CreateUpcastDelegateImpl(MethodInfo method) { + public static Func CreateUpcastDelegateImpl(MethodInfo method) { // Convert the reflection call into an open delegate, i.e. instead of calling x.Method() // we'll call getter(x). - Getter getter = (Getter)Delegate.CreateDelegate(typeof(Getter), method); + Func getter = (Func)Delegate.CreateDelegate(typeof(Func), 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. /// - public static SingleValueDelegate CreateDowncastDelegate(MethodInfo method) { + public static Action CreateDowncastDelegate(MethodInfo method) { MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateImpl"); MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType); - return (SingleValueDelegate)closedImpl.Invoke(null, new object[] { method }); + return (Action) closedImpl.Invoke(null, new object[] { method }); } - delegate void OpenSingleValueDelegate(TSource source, TParam parameter); - - public static SingleValueDelegate CreateDowncastDelegateImpl(MethodInfo method) { + public static Action CreateDowncastDelegateImpl(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 call = (OpenSingleValueDelegate) - Delegate.CreateDelegate(typeof(OpenSingleValueDelegate), method); + Action call = (Action) Delegate.CreateDelegate(typeof(Action), 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. /// - public static SingleValueDelegate CreateDowncastDelegateIgnoringReturn(MethodInfo method) { + public static Action CreateDowncastDelegateIgnoringReturn(MethodInfo method) { MethodInfo openImpl = typeof(ReflectionUtil).GetMethod("CreateDowncastDelegateIgnoringReturnImpl"); MethodInfo closedImpl = openImpl.MakeGenericMethod(typeof(T), method.GetParameters()[0].ParameterType, method.ReturnType); - return (SingleValueDelegate)closedImpl.Invoke(null, new object[] { method }); + return (Action)closedImpl.Invoke(null, new object[] { method }); } - delegate TReturn OpenSingleValueDelegate(TSource source, TParam parameter); - - public static SingleValueDelegate CreateDowncastDelegateIgnoringReturnImpl(MethodInfo method) { + public static Action CreateDowncastDelegateIgnoringReturnImpl(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 call = (OpenSingleValueDelegate) - Delegate.CreateDelegate(typeof(OpenSingleValueDelegate), method); + Func call = (Func) + Delegate.CreateDelegate(typeof(Func), method); return delegate(TSource source, object parameter) { call(source, (TParam)parameter); }; } - delegate T OpenCreateBuilderDelegate(); - /// /// Creates a delegate which will execute the given static method and cast the result up to IBuilder. /// - public static CreateBuilderDelegate CreateStaticUpcastDelegate(MethodInfo method) { + public static Func 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)closedImpl.Invoke(null, new object[] { method }); } - public static CreateBuilderDelegate CreateStaticUpcastDelegateImpl(MethodInfo method) { - OpenCreateBuilderDelegate call = (OpenCreateBuilderDelegate)Delegate.CreateDelegate(typeof(OpenCreateBuilderDelegate), method); + public static Func CreateStaticUpcastDelegateImpl(MethodInfo method) { + Func call = (Func)Delegate.CreateDelegate(typeof(Func), method); return delegate { return (IBuilder)call(); }; } } diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs index 5c62782b..fe801182 100644 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/RepeatedMessageAccessor.cs @@ -33,7 +33,7 @@ namespace Google.ProtocolBuffers.FieldAccess { /// in a message type "Foo", a field called "bar" might be of type "Baz". This /// method is Baz.CreateBuilder. /// - private readonly CreateBuilderDelegate createBuilderDelegate; + private readonly Func createBuilderDelegate; internal RepeatedMessageAccessor(string name) : base(name) { MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]); diff --git a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs index 0761635a..33def0a7 100644 --- a/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs @@ -26,11 +26,11 @@ namespace Google.ProtocolBuffers.FieldAccess { where TBuilder : IBuilder { private readonly Type clrType; - private readonly GetValueDelegate getValueDelegate; - private readonly ClearDelegate clearDelegate; - private readonly SingleValueDelegate addValueDelegate; - private readonly GetValueDelegate getRepeatedWrapperDelegate; - private readonly RepeatedCountDelegate countDelegate; + private readonly Func getValueDelegate; + private readonly Func clearDelegate; + private readonly Action addValueDelegate; + private readonly Func getRepeatedWrapperDelegate; + private readonly Func countDelegate; private readonly MethodInfo getElementMethod; private readonly MethodInfo setElementMethod; @@ -62,9 +62,9 @@ namespace Google.ProtocolBuffers.FieldAccess { || setElementMethod == null) { throw new ArgumentException("Not all required properties/methods available"); } - clearDelegate = (ClearDelegate)Delegate.CreateDelegate(typeof(ClearDelegate), clearMethod); - countDelegate = (RepeatedCountDelegate)Delegate.CreateDelegate - (typeof(RepeatedCountDelegate), countProperty.GetGetMethod()); + clearDelegate = (Func)Delegate.CreateDelegate(typeof(Func), clearMethod); + countDelegate = (Func)Delegate.CreateDelegate + (typeof(Func), countProperty.GetGetMethod()); getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetGetMethod()); addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn(addMethod); getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate(builderProperty.GetGetMethod()); diff --git a/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs index fd6b5c9b..931848b5 100644 --- a/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/SingleMessageAccessor.cs @@ -29,7 +29,7 @@ namespace Google.ProtocolBuffers.FieldAccess { /// in a message type "Foo", a field called "bar" might be of type "Baz". This /// method is Baz.CreateBuilder. /// - private readonly CreateBuilderDelegate createBuilderDelegate; + private readonly Func createBuilderDelegate; internal SingleMessageAccessor(string name) : base(name) { MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]); diff --git a/csharp/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs b/csharp/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs index 6c797370..c11a97dc 100644 --- a/csharp/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs +++ b/csharp/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs @@ -25,10 +25,11 @@ namespace Google.ProtocolBuffers.FieldAccess { where TBuilder : IBuilder { private readonly Type clrType; - private readonly GetValueDelegate getValueDelegate; - private readonly SingleValueDelegate setValueDelegate; - private readonly HasDelegate hasDelegate; - private readonly ClearDelegate clearDelegate; + private readonly Func getValueDelegate; + private readonly Action setValueDelegate; + private readonly Func hasDelegate; + private readonly Func clearDelegate; + delegate void SingleValueDelegate(TSource source, object value); /// /// The CLR type of the field (int, the enum type, ByteString, the message etc). @@ -47,8 +48,8 @@ namespace Google.ProtocolBuffers.FieldAccess { throw new ArgumentException("Not all required properties/methods available"); } clrType = messageProperty.PropertyType; - hasDelegate = (HasDelegate)Delegate.CreateDelegate(typeof(HasDelegate), hasProperty.GetGetMethod()); - clearDelegate = (ClearDelegate)Delegate.CreateDelegate(typeof(ClearDelegate), clearMethod); + hasDelegate = (Func)Delegate.CreateDelegate(typeof(Func), hasProperty.GetGetMethod()); + clearDelegate = (Func)Delegate.CreateDelegate(typeof(Func), clearMethod); getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetGetMethod()); setValueDelegate = ReflectionUtil.CreateDowncastDelegate(builderProperty.GetSetMethod()); }