C# 将泛型类型参数应用于返回泛型参数的方法时,MissingMethodException
我有一个名为DynamicVoke的方法,如下所示:C# 将泛型类型参数应用于返回泛型参数的方法时,MissingMethodException,c#,generics,exception,invoke,mono.cecil,C#,Generics,Exception,Invoke,Mono.cecil,我有一个名为DynamicVoke的方法,如下所示: public static object InvokeDynamic(Delegate d, object[] args) { Type[] tparams = d.Method.DeclaringType.GetGenericArguments() .Concat(d.Method.GetGenericArguments()) .ToAr
public static object InvokeDynamic(Delegate d, object[] args)
{
Type[] tparams = d.Method.DeclaringType.GetGenericArguments()
.Concat(d.Method.GetGenericArguments())
.ToArray();
Type dt = d.GetType().DeclaringType;
if (dt.ContainsGenericParameters)
dt = dt.MakeGenericType(tparams);
IDirectInvoke di = dt.GetConstructor(Type.EmptyTypes).Invoke(null) as IDirectInvoke;
object o = di.Invoke(d.Method, d.Target, args);
return o;
}
它在以下上下文中使用(是的,这是使用Mono.Cecil编程生成的代码,并使用ILSpy反转):
考虑到我已经在ILSpy中检查了结果,并且IL和元数据在功能上与C#编译器本身生成的相同(通过将代码复制到.cs文件并进行编译进行测试),我现在认为是运行时使用Invoke方法导致了这个问题
一个奇怪的效果是,构造函数被成功调用,VisualStudio在检查新创建的IDirectInvoke对象时正确地报告了类型(它只在调用Invoke时抛出异常),但正如前面提到的,Invoke的IL与C#生成的完全相同
有人知道在这种情况下会出现什么问题吗
编辑:还有一件事,这只发生在直接返回分配给方法的泛型参数的方法上(因此,如果该方法返回a、B或另一个包含a、B或C参数的泛型类型实例,则不会发生这种情况;在这种情况下,发生这种情况是因为该方法直接返回C类型的对象)
[Processed, Distributed]
[Serializable]
public class GenericsTest<A, B> : ITransparent, ISerializable where A : new() where B : ITest
{
private class Method3__InvokeDirect3<C> : IDirectInvoke
{
[CompilerGenerated]
public delegate C Method3__DistributedDelegate4();
public object Invoke(MethodInfo method, object instance, object[] parameters)
{
GenericsTest<A, B>.Method3__InvokeDirect3<C>.Method3__DistributedDelegate4 method3__DistributedDelegate = (GenericsTest<A, B>.Method3__InvokeDirect3<C>.Method3__DistributedDelegate4)Delegate.CreateDelegate(typeof(GenericsTest<A, B>.Method3__InvokeDirect3<C>.Method3__DistributedDelegate4), instance, method);
return method3__DistributedDelegate.Invoke();
}
}
public C Method3<C>()
{
MulticastDelegate d = new GenericsTest<A, B>.Method3__InvokeDirect3<C>.Method3__DistributedDelegate4(this.Method3__Distributed0<C>);
object[] args = new object[0];
return (C)DpmEntrypoint.Invoke(d, args);
}
[CompilerGenerated]
private C Method3__Distributed0<C>()
{
return default(C);
}
}
System.MissingMethodException was unhandled
Message="Method not found: '**UNKNOWN TYPE** Method3__DistributedDelegate4.Invoke()'."
Source="Examples.ServerClient"
StackTrace:
at Examples.ServerClient.GenericsTest`2.Method3__InvokeDirect3`1.Invoke(MethodInfo method, Object instance, Object[] parameters)
at Process4.Providers.DpmEntrypoint.InvokeDynamic(Delegate d, Object[] args) in C:\Server Storage\Projects\Redpoint\Dx\Process4\Providers\Dpm.cs:line 249
at Process4.Providers.DpmEntrypoint.Invoke(Delegate d, Object[] args) in C:\Server Storage\Projects\Redpoint\Dx\Process4\Providers\Dpm.cs:line 359
at Examples.ServerClient.GenericsTest`2.Method3[C]()
at Examples.ServerClient.Program.GenericsTest() in C:\Server Storage\Projects\Redpoint\Dx\Examples.ServerClient\Program.cs:line 76
at Examples.ServerClient.Program.Main(String[] args) in C:\Server Storage\Projects\Redpoint\Dx\Examples.ServerClient\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()