3

我有一个名为 DynamicInvoke 的方法,如下所示:

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 反转):

[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);
    }
}

现在,如果 a) GenericsTest 没有泛型参数或 b) 该方法没有泛型参数(并且我有其他类和方法可以验证这一点),则代码现在可以正常运行。只有当类和方法都包含泛型参数时才会出现问题。

调用 di.Invoke 时,出现以下异常:

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()

鉴于我在 ILSpy 中检查了结果,并且 IL 和元数据在功能上等同于 C# 编译器本身生成的结果(通过将代码复制到 .cs 文件并编译它进行测试),我现在认为它是导致此问题的 Invoke 方法的运行时使用。

一个奇怪的效果是构造函数被成功调用,并且 Visual Studio 在检查新创建的 IDirectInvoke 对象时正确报告了类型(它只在调用 Invoke 时抛出异常),但如前所述,Invoke 的 IL 与C# 生成。

有谁知道在这种情况下可能是什么问题?

编辑:还有一件事,这只发生在直接返回分配给方法的泛型参数的方法上(因此,如果方法返回 A、B 或另一个包含 A、B 或 C 参数的泛型类型实例,则不会发生这种情况;在这种情况发生是因为该方法直接返回了一个 C 类型的对象)。

4

0 回答 0