diff --git a/src/ProtocolBuffers.Test/TestUtil.cs b/src/ProtocolBuffers.Test/TestUtil.cs index 12d73d165a..ffbb91d5b3 100644 --- a/src/ProtocolBuffers.Test/TestUtil.cs +++ b/src/ProtocolBuffers.Test/TestUtil.cs @@ -1823,6 +1823,13 @@ namespace Google.ProtocolBuffers action(); Assert.Fail("Exception was not thrown"); } + // Not a general case, however, Compact Framework v2 does use Invoke + catch (System.Reflection.TargetInvocationException te) + { + if (te.InnerException.GetType() != typeof(ArgumentNullException)) + throw; + } + // Normally expected exception catch (ArgumentNullException) { // We expect this exception. diff --git a/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs index 92e8a07caf..d2b6c51f36 100644 --- a/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs +++ b/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs @@ -70,8 +70,7 @@ namespace Google.ProtocolBuffers.FieldAccess { // Convert the reflection call into an open delegate, i.e. instead of calling x.Method() // we'll call getter(x). - Func getter = - (Func)FrameworkPortability.CreateDelegate(typeof(Func), null, method); + Func getter = ReflectionUtil.CreateDelegateFunc(method); // Implicit upcast to object (within the delegate) return delegate(TSource source) { return getter(source); }; @@ -92,8 +91,7 @@ namespace Google.ProtocolBuffers.FieldAccess { // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll // call Method(x, y) - Action call = - (Action)FrameworkPortability.CreateDelegate(typeof(Action), null, method); + Action call = ReflectionUtil.CreateDelegateAction(method); return delegate(TSource source, object parameter) { call(source, (TParam) parameter); }; } @@ -115,9 +113,7 @@ namespace Google.ProtocolBuffers.FieldAccess { // Convert the reflection call into an open delegate, i.e. instead of calling x.Method(y) we'll // call Method(x, y) - Func call = (Func) - FrameworkPortability.CreateDelegate(typeof(Func), null, - method); + Func call = ReflectionUtil.CreateDelegateFunc(method); return delegate(TSource source, object parameter) { call(source, (TParam) parameter); }; } @@ -134,8 +130,61 @@ namespace Google.ProtocolBuffers.FieldAccess public static Func CreateStaticUpcastDelegateImpl(MethodInfo method) { - Func call = (Func)FrameworkPortability.CreateDelegate(typeof(Func), null, method); + Func call = ReflectionUtil.CreateDelegateFunc(method); return delegate { return (IBuilder) call(); }; } + + + internal static Func CreateDelegateFunc(MethodInfo method) + { +#if !NOCREATEDELEGATE + object tdelegate = Delegate.CreateDelegate(typeof(Func), null, method, true); + return (Func)tdelegate; +#else + return delegate() { return (TResult)method.Invoke(null, null); }; +#endif + } + + internal static Func CreateDelegateFunc(MethodInfo method) + { +#if !NOCREATEDELEGATE + object tdelegate = Delegate.CreateDelegate(typeof(Func), null, method, true); + return (Func)tdelegate; +#else + if (method.IsStatic) + { + return delegate(T arg1) { return (TResult) method.Invoke(null, new object[] {arg1}); }; + } + return delegate(T arg1) { return (TResult)method.Invoke(arg1, null); }; +#endif + } + + internal static Func CreateDelegateFunc(MethodInfo method) + { +#if !NOCREATEDELEGATE + object tdelegate = Delegate.CreateDelegate(typeof(Func), null, method, true); + return (Func)tdelegate; +#else + if (method.IsStatic) + { + return delegate(T1 arg1, T2 arg2) { return (TResult) method.Invoke(null, new object[] {arg1, arg2}); }; + } + return delegate(T1 arg1, T2 arg2) { return (TResult)method.Invoke(arg1, new object[] { arg2 }); }; +#endif + } + + internal static Action CreateDelegateAction(MethodInfo method) + { +#if !NOCREATEDELEGATE + object tdelegate = Delegate.CreateDelegate(typeof(Action), null, method, true); + return (Action)tdelegate; +#else + if (method.IsStatic) + { + return delegate(T1 arg1, T2 arg2) { method.Invoke(null, new object[] {arg1, arg2}); }; + } + return delegate(T1 arg1, T2 arg2) { method.Invoke(arg1, new object[] { arg2 }); }; +#endif + } } } \ No newline at end of file diff --git a/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs b/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs index 0a173fea33..d3b926bcc9 100644 --- a/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs +++ b/src/ProtocolBuffers/FieldAccess/RepeatedPrimitiveAccessor.cs @@ -84,9 +84,8 @@ namespace Google.ProtocolBuffers.FieldAccess { throw new ArgumentException("Not all required properties/methods available"); } - clearDelegate = (Func) - FrameworkPortability.CreateDelegate(typeof(Func), null, clearMethod); - countDelegate = (Func)FrameworkPortability.CreateDelegate(typeof(Func), null, countProperty.GetGetMethod()); + clearDelegate = ReflectionUtil.CreateDelegateFunc(clearMethod); + countDelegate = ReflectionUtil.CreateDelegateFunc(countProperty.GetGetMethod()); getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetGetMethod()); addValueDelegate = ReflectionUtil.CreateDowncastDelegateIgnoringReturn(addMethod); getRepeatedWrapperDelegate = ReflectionUtil.CreateUpcastDelegate(builderProperty.GetGetMethod()); diff --git a/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs b/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs index e52af03dce..e5a07540b0 100644 --- a/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs +++ b/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs @@ -67,11 +67,8 @@ namespace Google.ProtocolBuffers.FieldAccess throw new ArgumentException("Not all required properties/methods available"); } clrType = messageProperty.PropertyType; - hasDelegate = - (Func) - FrameworkPortability.CreateDelegate(typeof(Func), null, hasProperty.GetGetMethod()); - clearDelegate = - (Func)FrameworkPortability.CreateDelegate(typeof(Func), null, clearMethod); + hasDelegate = ReflectionUtil.CreateDelegateFunc(hasProperty.GetGetMethod()); + clearDelegate = ReflectionUtil.CreateDelegateFunc(clearMethod); getValueDelegate = ReflectionUtil.CreateUpcastDelegate(messageProperty.GetGetMethod()); setValueDelegate = ReflectionUtil.CreateDowncastDelegate(builderProperty.GetSetMethod()); }