Permalink
Browse files

NReco: DelegateAdapter fix for delegates without return type

  • Loading branch information...
VitaliyMF committed Jun 3, 2014
1 parent f709d4f commit 734be69852a04b57ed01ae113f2163c415d7ab5e
@@ -197,6 +197,11 @@ public class ConvertersTests {
Assert.True( dConv.CanConvert( constPrv.GetType(), typeof(Func<int,int>) ) );
var getConstDeleg = (Func<int,int>) dConv.Convert(constPrv, typeof(Func<int,int>) );
Assert.AreEqual( "5", getConstDeleg(5).ToString() );
Action<int> a1 = (a) => { };
Assert.True(dConv.CanConvert(typeof(Action<int>), typeof(Action<object>)));
var castAction = dConv.Convert(a1, typeof(Action<object>) );
}
public delegate bool CustomDelegateType(string param);
@@ -68,10 +68,6 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="SemWeb, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\SemWeb.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -93,10 +89,6 @@
<Compile Include="Web\SiteControlExtensionTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NReco.Dsm.WebForms\NReco.Dsm.WebForms.csproj">
<Project>{023df467-7818-45ff-8be0-e1d9800514e3}</Project>
<Name>NReco.Application.Web.Forms</Name>
</ProjectReference>
<ProjectReference Include="..\NReco.Application\NReco.Application.csproj">
<Project>{18a40556-9427-46b8-bc81-db371b5d0119}</Project>
<Name>NReco.Application</Name>
@@ -105,6 +97,10 @@
<Project>{ed246002-abb6-46bc-9188-9a18b236878e}</Project>
<Name>NReco.Dsm.Composition</Name>
</ProjectReference>
<ProjectReference Include="..\NReco.Dsm.WebForms\NReco.Dsm.WebForms.csproj">
<Project>{023df467-7818-45ff-8be0-e1d9800514e3}</Project>
<Name>NReco.Dsm.WebForms</Name>
</ProjectReference>
<ProjectReference Include="..\NReco.Transform\NReco.Transform.csproj">
<Project>{DF79683A-3701-4448-8A8E-702F573AE06F}</Project>
<Name>NReco.Transform</Name>
@@ -10,8 +10,7 @@
using NUnit.Framework;
using NReco;
using NReco.Collections;
using NReco.Application.Web;
using NReco.Application.Web.Forms;
using NReco.Dsm.WebForms;
namespace NReco.Tests {
@@ -19,9 +19,10 @@ public abstract class DelegateAdapter {
static MethodInfo[] invokeAdapterMethods = typeof(DelegateAdapter).GetMethods();
public MethodInfo GetInvokeGenericMethod(int argsCount) {
public MethodInfo GetInvokeGenericMethod(int argsCount, bool hasReturn) {
var methodName = hasReturn ? "InvokeFunc" : "InvokeAction";
foreach (var adapterMethod in invokeAdapterMethods) {
if (adapterMethod.Name == "Invoke" && adapterMethod.GetParameters().Length == argsCount) {
if (adapterMethod.Name == methodName && adapterMethod.GetParameters().Length == argsCount) {
return adapterMethod;
}
}
@@ -39,62 +40,109 @@ public abstract class DelegateAdapter {
var toMethodInfo = delegType.GetMethod("Invoke");
var toMethodParams = toMethodInfo.GetParameters();
var toMethodParamCount = toMethodParams.Length;
var hasReturn = toMethodInfo.ReturnType != typeof(void);
var invokeGenericMethod = GetInvokeGenericMethod(toMethodParamCount);
var invokeGenericMethod = GetInvokeGenericMethod(toMethodParamCount, hasReturn);
var resType = toMethodInfo.ReturnType != typeof(void) ? toMethodInfo.ReturnType : typeof(object);
var genericTypes = new Type[toMethodParamCount + 1];
var genericTypes = new Type[toMethodParamCount + (hasReturn ? 1 : 0)];
for (int i = 0; i < toMethodParams.Length; i++)
genericTypes[i] = toMethodParams[i].ParameterType;
genericTypes[toMethodParamCount] = resType; // last type for result
if (hasReturn)
genericTypes[toMethodParamCount] = toMethodInfo.ReturnType; // last type for result
var typedInvokeMethod = invokeGenericMethod.MakeGenericMethod(genericTypes);
var typedInvokeMethod = genericTypes.Length>0 ? invokeGenericMethod.MakeGenericMethod(genericTypes) : invokeGenericMethod;
return Delegate.CreateDelegate(delegType, this, typedInvokeMethod, true);
}
public T Invoke<T>() {
public void InvokeAction() {
Invoke();
}
public void InvokeAction<P1>(P1 arg1) {
Invoke(arg1);
}
public void InvokeAction<P1,P2>(P1 arg1, P2 arg2) {
Invoke(arg1,arg2);
}
public void InvokeAction<P1, P2, P3>(P1 arg1, P2 arg2, P3 arg3) {
Invoke(arg1, arg2, arg3);
}
public void InvokeAction<P1, P2, P3, P4>(P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
Invoke(arg1, arg2, arg3, arg4);
}
public void InvokeAction<P1, P2, P3, P4, P5>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5) {
Invoke(arg1, arg2, arg3, arg4, arg5);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7, P8>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7, P8, P9>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
}
public void InvokeAction<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13, P14 arg14) {
Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
}
public T InvokeFunc<T>() {
return (T)ConvertManager.ChangeType(Invoke(), typeof(T));
}
public T Invoke<P1, T>(P1 arg1) {
public T InvokeFunc<P1, T>(P1 arg1) {
return (T)ConvertManager.ChangeType(Invoke(arg1), typeof(T));
}
public T Invoke<P1, P2, T>(P1 arg1, P2 arg2) {
public T InvokeFunc<P1, P2, T>(P1 arg1, P2 arg2) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2), typeof(T));
}
public T Invoke<P1, P2, P3, T>(P1 arg1, P2 arg2, P3 arg3) {
public T InvokeFunc<P1, P2, P3, T>(P1 arg1, P2 arg2, P3 arg3) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3), typeof(T));
}
public T Invoke<P1, P2, P3, P4, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
public T InvokeFunc<P1, P2, P3, P4, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5) {
public T InvokeFunc<P1, P2, P3, P4, P5, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, P8, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, P8, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, P8, P9, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, P8, P9, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13), typeof(T));
}
public T Invoke<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13, P14 arg14) {
public T InvokeFunc<P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, T>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13, P14 arg14) {
return (T)ConvertManager.ChangeType(Invoke(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14), typeof(T));
}

0 comments on commit 734be69

Please sign in to comment.