C# 绑定到目标方法时出错 MethodInfo method=typeof(T).GetMethod(“Parse”,new[]{typeof(string)}); parse=Delegate.CreateDelegate(typeof(Func),方法);
在这种情况下,T是一个浮点数。但是,绑定到目标方法时出现错误。我认为Parse是一种静态方法。我已经看了其他示例,但我不明白为什么它没有绑定。您必须交换C# 绑定到目标方法时出错 MethodInfo method=typeof(T).GetMethod(“Parse”,new[]{typeof(string)}); parse=Delegate.CreateDelegate(typeof(Func),方法);,c#,reflection,binding,dynamic,C#,Reflection,Binding,Dynamic,在这种情况下,T是一个浮点数。但是,绑定到目标方法时出现错误。我认为Parse是一种静态方法。我已经看了其他示例,但我不明白为什么它没有绑定。您必须交换T和string,因为该方法返回的是T而不是string 我将T替换为float,以下代码适用于我: MethodInfo method = typeof(T).GetMethod("Parse", new[] { typeof(string) }); parse = Delegate.CreateDelegate(typeof(Func<
T
和string
,因为该方法返回的是T
而不是string
我将T
替换为float
,以下代码适用于我:
MethodInfo method = typeof(T).GetMethod("Parse", new[] { typeof(string) });
parse = Delegate.CreateDelegate(typeof(Func<T,string>), method);
MethodInfo method=typeof(float).GetMethod(“Parse”,BindingFlags.Static | BindingFlags.Public,null,new[]{typeof(string)},null);
var parse=Delegate.CreateDelegate(typeof(Func),方法);
来源:VS intellisense和MSDN请给出一个简短但完整的程序来演示这个问题。我刚刚使用了委托方法backward MethodInfo method=typeof(T).GetMethod(“Parse”,new[]{typeof(string)});parse=Delegate.CreateDelegate(typeof(Func),方法);这只是重复问题中的内容。一个简短但完整的程序是我们可以编译并运行的,无需进一步修改。不仅仅是泛型类型/方法的一部分。哇,这是一个非常简单的问题。谢谢我只是绞尽脑汁,但没找这么小的东西。
MethodInfo method = typeof(float).GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, null);
var parse = Delegate.CreateDelegate(typeof(Func<string, float>), method);