gpt4 book ai didi

c# - CreateDelegate 拒绝为实例方法创建委托(delegate)

转载 作者:行者123 更新时间:2023-11-30 14:37:00 25 4
gpt4 key购买 nike

这是 prior thread 的一种后续。我正在构建一个小型包装器来向上调用我的用户提供的动态类型化方法。该方案运作良好......但仅适用于静态方法。尽管 CreateDelegate 也应该适用于实例方法,但当与这些方法一起使用时,如果方法 isStatic 标志为假,它会抛出“绑定(bind)错误”(实际上,因为我的 throw-on-error 标志为假,它返回 null) .这是一个代码示例,您可以在其中看到这种情况。

using System; 
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace ConsoleApplication4
{
delegate void myFoo(int i, string s);
delegate void myNull();

internal class Callable
{
internal int nParams;
internal Type[] ptypes;
internal Delegate cb;
internal static Type[] actions = { typeof(Action), typeof(Action<>), typeof(Action<,>), typeof(Action<,,>), typeof(Action<,,,>), typeof(Action<,,,,>),
typeof(Action<,,,,,>), typeof(Action<,,,,,,>), typeof(Action<,,,,,,,>), typeof(Action<,,,,,,,,>), typeof(Action<,,,,,,,,,>),
typeof(Action<,,,,,,,,,,>), typeof(Action<,,,,,,,,,,,>), typeof(Action<,,,,,,,,,,,,>), typeof(Action<,,,,,,,,,,,,,>), typeof(Action<,,,,,,,,,,,,,,>) };

internal Callable(Delegate hisCb)
{
MethodInfo mi = hisCb.Method;
ParameterInfo[] pi = mi.GetParameters();
ptypes = pi.Select(p => p.ParameterType).ToArray();
nParams = ptypes.Length;
if (nParams > 0 && nParams < 17)
{
cb = Delegate.CreateDelegate(actions[nParams].MakeGenericType(ptypes), mi, false);
if (cb == null)
Console.WriteLine("Warning: Unsuccessful attempt to CreateDelegate for " + hisCb + ", with methodinfo " + mi);
else
Console.WriteLine("Successful attempt to CreateDelegate for " + hisCb + ", with methodinfo " + mi);
}
else
cb = hisCb;
}

internal void doUpcall(object[] args)
{
if (args.Length != nParams)
throw new ArgumentException("Argument count must match number of parameters");
switch (nParams)
{
case 1:
((dynamic)cb).Invoke((dynamic)args[0]);
break;
case 2:
((dynamic)cb).Invoke((dynamic)args[0], (dynamic)args[1]);
break;
// ... cases 3-15 similar, omitted to save space
default:
cb.DynamicInvoke((dynamic)args);
break;
}
}
}

internal class FooBar
{
internal FooBar()
{
}

internal static void printFields(int i, string s)
{
Console.WriteLine("In FooBar.printField-s with i="+i+", s="+s);
}

internal void printFieldi(int i, string s)
{
Console.WriteLine("In FooBar.printField-i with i=" + i + ", s=" + s);
}
}

internal class Program
{
private static void Main(string[] args)
{
FooBar myFooBar = new FooBar();
Callable cbfb0 = new Callable((myFoo)FooBar.printFields);
cbfb0.doUpcall(new object[] { 77, "myfb" });
Callable cbfb1 = new Callable((myFoo)myFooBar.printFieldi);
cbfb1.doUpcall(new object[] { 77, "myfb" });
string pc = "Main";
Callable cb0 = new Callable((myNull)delegate() { Console.WriteLine("Hello from myNull"); });
cb0.doUpcall(new object[0]);
Callable cb1 = new Callable((myFoo)delegate(int i, string s) { Console.WriteLine("i=" + i + ", s.Length = " + s.Length); });
Console.WriteLine("About to attempt to call Foo: Good args");
cb1.doUpcall(new object[] { 2, "bar" });
Console.WriteLine("After calling Foo");
Callable cb2 = new Callable((myFoo)delegate(int i, string s) { Console.WriteLine("My parent class is " + pc + ", i=" + i + ", s.Length = " + s.Length); });
Console.WriteLine("About to attempt to call Foo: Good args");
cb2.doUpcall(new object[] { 12, "Bar" });
Console.WriteLine("After calling Foo");
System.Threading.Thread.Sleep(15000);
}

private static void Foo(int i, string s)
{
Console.WriteLine("i=" + i + ", s.Length = " + s.Length);
}
}
}

谁能帮我理解为什么 CreateDelegate 会这样? C# 和 .NET 引用信息说它应该适用于静态方法和实例方法。如果您打破“不成功”的情况,您可以确认决定成功或失败的是 mi.isStatic 标志的值。

PS:注意在运行时使用 (dynamic) 将参数转换为所需的类型!我认为这很酷。过去是不可能的——您想进行强制转换 (T),但不知道 T 的类型是什么,因此可以创建 T 类型的对象,但不能动态调用使用该对象的方法,就像我的 15 个案例陈述一样。通过强制转换为 (dynamic) 我避免了这个问题——解决了一个似乎有几十个旧线程 Unresolved 问题! (这改进了先前线程中建议的代码......它具有使用已知类型进行转换的相同问题)。

最佳答案

CreateDelegate 创建一个可调用委托(delegate) - 为此,它必须具有调用方法的实例。对于静态方法,不需要实例 - 您正在调用允许创建静态方法委托(delegate)的重载。

要在实例方法上创建委托(delegate),您必须使用允许您也传入实例的覆盖:

 cb = Delegate.CreateDelegate(actions[nParams].MakeGenericType(ptypes), 
hisCb.Target,
mi, false);

关于c# - CreateDelegate 拒绝为实例方法创建委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9855356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com