From 6f82b2f2a4cda4f7da03ce55386b3e2d7e090915 Mon Sep 17 00:00:00 2001 From: David Tchepak Date: Wed, 4 Jan 2017 10:06:50 +1100 Subject: [PATCH] Fix NET35 build NET35 doesn't seem to support Expression.Variable. Added back previous DelegateProxyFactory implementation from ac140ef7ba3d684ce2be1d0e3d3ff93bc5cc0373 for NET35 target only. Updated test accordingingly. --- .../Issue271_DelegateOutArgument.cs | 4 ++++ .../DelegateProxy/DelegateProxyFactory.cs | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Source/NSubstitute.Acceptance.Specs/FieldReports/Issue271_DelegateOutArgument.cs b/Source/NSubstitute.Acceptance.Specs/FieldReports/Issue271_DelegateOutArgument.cs index 4a03748..e910f3c 100644 --- a/Source/NSubstitute.Acceptance.Specs/FieldReports/Issue271_DelegateOutArgument.cs +++ b/Source/NSubstitute.Acceptance.Specs/FieldReports/Issue271_DelegateOutArgument.cs @@ -15,7 +15,11 @@ namespace NSubstitute.Acceptance.Specs.FieldReports foo(out bar); +#if NET4 || NET45 Assert.AreEqual(42, bar); +#else + Assert.Ignore("Not supported for NET35"); +#endif } } } \ No newline at end of file diff --git a/Source/NSubstitute/Proxies/DelegateProxy/DelegateProxyFactory.cs b/Source/NSubstitute/Proxies/DelegateProxy/DelegateProxyFactory.cs index b06a13a..f17e98f 100644 --- a/Source/NSubstitute/Proxies/DelegateProxy/DelegateProxyFactory.cs +++ b/Source/NSubstitute/Proxies/DelegateProxy/DelegateProxyFactory.cs @@ -41,6 +41,28 @@ namespace NSubstitute.Proxies.DelegateProxy ParameterExpression[] proxyParameters = delegateMethodToProxy.GetParameters().Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray(); Expression[] proxyParametersAsObjects = proxyParameters.Select(x => (Expression)Expression.Convert(x, typeof(object))).ToArray(); +#if NET35 + Expression callInvokeOnDelegateCallInstance = + Expression.Call( + Expression.Constant(delegateCall), + invokeOnDelegateCall, + new Expression[] + { + Expression.NewArrayInit(typeof(object), proxyParametersAsObjects) + } + ); + + if (delegateMethodToProxy.ReturnType != typeof(void)) + { + callInvokeOnDelegateCallInstance = + Expression.Convert(callInvokeOnDelegateCallInstance, delegateMethodToProxy.ReturnType); + } + + var proxyExpression = Expression.Lambda(delegateType, callInvokeOnDelegateCallInstance, proxyParameters); + return proxyExpression.Compile(); +#endif +#if !NET35 + var bodyExpressions = new List(); bool isVoid = delegateMethodToProxy.ReturnType == typeof(void); var arguments = Expression.Variable(typeof(object[]), "arguments"); @@ -71,6 +93,7 @@ namespace NSubstitute.Proxies.DelegateProxy var variables = isVoid ? new[] { arguments } : new[] { arguments, result }; var proxyExpression = Expression.Lambda(delegateType, Expression.Block(variables, bodyExpressions), proxyParameters); return proxyExpression.Compile(); +#endif } } } \ No newline at end of file