Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到目标方法时出错 MethodInfo method=typeof(T).GetMethod(“Parse”,new[]{typeof(string)}); parse=Delegate.CreateDelegate(typeof(Func),方法);_C#_Reflection_Binding_Dynamic - Fatal编程技术网

C# 绑定到目标方法时出错 MethodInfo method=typeof(T).GetMethod(“Parse”,new[]{typeof(string)}); parse=Delegate.CreateDelegate(typeof(Func),方法);

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是一个浮点数。但是,绑定到目标方法时出现错误。我认为Parse是一种静态方法。我已经看了其他示例,但我不明白为什么它没有绑定。

您必须交换
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);