MethodGroup 作为通用参数

发布于 2025-01-03 20:34:24 字数 594 浏览 2 评论 0原文

我正在尝试编写一个从给定输入返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2025-01-10 20:34:25

你可以这样做,但我很困惑为什么你需要指定委托类型。如果没有它,它就不起作用,但由于下面的代码适用于 BarMethod,所以委托类型是什么似乎并不重要:

public static void Main(String[] args)
{
    Foo f = new Foo();
    Console.WriteLine(ForMethod(() => f.FooMethod()).Name);
    Console.WriteLine(ForMethod(() => f.Foo2Method<String>()).Name);
    Console.WriteLine(ForMethod(() => f.BarMethod("foo")).Name);
    Console.ReadKey();
}

public static MethodInfo ForMethod(Expression<Action> e)
{
    var mi = ((MethodCallExpression) e.Body).Method;
    if (mi.IsGenericMethod)
        mi = mi.GetGenericMethodDefinition();
    return mi;
}

class Foo
{
    public void FooMethod() { }
    public void Foo2Method<T>() { }
    public void BarMethod(String foo) { }
}

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:

public static void Main(String[] args)
{
    Foo f = new Foo();
    Console.WriteLine(ForMethod(() => f.FooMethod()).Name);
    Console.WriteLine(ForMethod(() => f.Foo2Method<String>()).Name);
    Console.WriteLine(ForMethod(() => f.BarMethod("foo")).Name);
    Console.ReadKey();
}

public static MethodInfo ForMethod(Expression<Action> e)
{
    var mi = ((MethodCallExpression) e.Body).Method;
    if (mi.IsGenericMethod)
        mi = mi.GetGenericMethodDefinition();
    return mi;
}

class Foo
{
    public void FooMethod() { }
    public void Foo2Method<T>() { }
    public void BarMethod(String foo) { }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文