如何反映没有参数的方法?
我正在尝试获取签名中带有 out
参数的类型的方法的 MethodInfo
对象。其效果如下:
MethodInfo tryParse = typeof(T).GetMethod(
"TryParse",
BindingFlags.Public|BindingFlags.Static,
null,
new Type[] { typeof(string), typeof(T) },
null);
但问题是,它找不到它,因为第二个参数的类型不仅仅是 T
而是 out T
。当我调试并使用 typeof(T).GetMethods()
时,我可以看到我想要的实际 MethodInfo
,并且 ParameterInfo
对象是类型为 T&
或 T ByRef
,但我看不到如何从 typeof(T )
。
有什么想法吗?
I am trying to get a MethodInfo
object for a method on a type with an out
param in its signature. Something to the effect of this:
MethodInfo tryParse = typeof(T).GetMethod(
"TryParse",
BindingFlags.Public|BindingFlags.Static,
null,
new Type[] { typeof(string), typeof(T) },
null);
But the problem is, it doesn't find it because the type of the second parameter is not simply T
but out T
. When I debug through and use typeof(T).GetMethods()
I can see the actual MethodInfo
that I want and the ParameterInfo
object is either of type T&
or T ByRef
, but I can't see how to create the Type
that represents this from typeof(T)
.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何有这个问题的人接下来会遇到的是“好吧,但是我如何调用它!?!” 这篇文章为我澄清了这一点。简短回答:参数数组包含输出参数,而不是用于填充参数数组的变量。
And the very next thing anyone with this question is going to run into is "Okay but how to I invoke it!?!" This article cleared that up for me. Short answer: the arguments array contains the out param, not the variable you used to populate the arguments array.