Skip to content
This repository has been archived by the owner on Aug 17, 2024. It is now read-only.

Commit

Permalink
NReco: DelegateAdapter fix for delegates without return type
Browse files Browse the repository at this point in the history
  • Loading branch information
VitaliyMF committed Jun 3, 2014
1 parent f709d4f commit 734be69
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 32 deletions.
5 changes: 5 additions & 0 deletions src/NReco.Tests/ConvertersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ public void DelegateConverterTest() {
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);
Expand Down
12 changes: 4 additions & 8 deletions src/NReco.Tests/NReco.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down
3 changes: 1 addition & 2 deletions src/NReco.Tests/Web/SiteControlExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
92 changes: 70 additions & 22 deletions src/NReco/DelegateAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public 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;
}
}
Expand All @@ -39,62 +40,109 @@ public Delegate GetDelegate(Type delegType) {
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));
}

Expand Down

0 comments on commit 734be69

Please sign in to comment.