MethodGroup 作为通用参数
我正在尝试编写一个从给定输入返回 methodinfo 类的方法,以创建该方法的通用版本...即
var m = myClass.GetType().GetMethod("SomeMethod").MakeGenericMethod(...blahblah..);
这有效并且一切都很好,除了我有我的方法名称的字符串文字,因此,如果在重构过程中我碰巧重命名了我正在使用的方法之一,我直到运行时才会发现。
我想要做的是创建一个辅助方法,我可以将指定方法组的lambda传递给它,这样我就可以对方法名称进行编译时检查,更不用说智能感知等......即。
MethodInfo mi = myClass.GetMethodInfo( o => o.SomeMethod );
m = mi.MakeGenericMethod(..blah...);
但我一直无法弄清楚助手的方法签名......
public MethodInfo GetMethodInfo(Func<MyClass,XXXX> lambda){ //What is my XXXX ? }
I am trying to write a method that returns a methodinfo class from a given input, with a view to creating the generic version of that method...i.e.
var m = myClass.GetType().GetMethod("SomeMethod").MakeGenericMethod(...blahblah..);
This works and is all good, except that I have the string literal of my method name, so if in the course of re-factoring I happen to rename one of the methods I am using I don't find out until run time.
What I would like to do is create a helper method that I can pass a lamba to that specifies the methodgroup, that way I would get compile time checking of the method name, not to mention intellisense etc...ie.
MethodInfo mi = myClass.GetMethodInfo( o => o.SomeMethod );
m = mi.MakeGenericMethod(..blah...);
But I haven't been able to figure out the method signature of the helper...
public MethodInfo GetMethodInfo(Func<MyClass,XXXX> lambda){ //What is my XXXX ? }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做,但我很困惑为什么你需要指定委托类型。如果没有它,它就不起作用,但由于下面的代码适用于
BarMethod
,所以委托类型是什么似乎并不重要:You can do it like this, but I am stumped as to why you need the to specify the delegate type. Without that it doesn't work, but since the below works for
BarMethod
, it doesn't seem to matter what the delegate type is: