通过MethodInfo调用方法

发布于 2024-07-21 15:53:48 字数 551 浏览 8 评论 0原文

我想调用具有特定属性的方法。 因此,我循环遍历所有程序集和所有方法,以查找具有我的属性的方法。 工作正常,但是当我只得到某个方法的 MethodInfo 时,如何调用它?

AppDomain app = AppDomain.CurrentDomain;
Assembly[] ass = app.GetAssemblies();
Type[] types;
foreach (Assembly a in ass)
{
    types = a.GetTypes();
    foreach (Type t in types)
    {
        MethodInfo[] methods = t.GetMethods();
        foreach (MethodInfo method in methods)
        {
            // Invoke a certain method
        }
    }
}

问题是我不知道包含该特定方法的类的实例。 所以我无法正确调用它,因为这些方法不是静态的。 如果可能的话,我还想避免创建此类的新实例。

I want to invoke methods with a certain attribute.
So I'm cycling through all the assemblies and all methods to find the methods with my attribute. Works fine, but how do I invoke a certain method when I only got it's MethodInfo.

AppDomain app = AppDomain.CurrentDomain;
Assembly[] ass = app.GetAssemblies();
Type[] types;
foreach (Assembly a in ass)
{
    types = a.GetTypes();
    foreach (Type t in types)
    {
        MethodInfo[] methods = t.GetMethods();
        foreach (MethodInfo method in methods)
        {
            // Invoke a certain method
        }
    }
}

The problem is that I don't know the instance of the class that contains that certain method. So I can't invoke it properly because the methods are not static.
I also want to avoid creating a new instance of this class if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

芯好空 2024-07-28 15:53:48

非静态方法是特定于实例的,因此您必须实例化该类才能调用该方法。 如果您有能力更改定义它的代码,并且该方法不需要自身成为实例的一部分(它不访问或修改类内的任何非静态属性或方法),那么最佳实践是无论如何都要使方法静态。

假设您不能使其静态,那么您需要的代码如下:

    foreach (Type t in types)
    {
            object instance = Activator.CreateInstance(t);

            MethodInfo[] methods = t.GetMethods();
            foreach (MethodInfo method in methods)
            {                    
                method.Invoke(instance, params...);    
            }
    }

Non-static methods are instance specific so you must instantiate the class to invoke the method. If you have the ability to change the code where it is defined and the method doesn't require itself to be part of an instance (it doesn't access or modify any non-static properties or methods inside the class) then best practice would be to make the method static anyway.

Assuming you can't make it static then the code you need is as follows:

    foreach (Type t in types)
    {
            object instance = Activator.CreateInstance(t);

            MethodInfo[] methods = t.GetMethods();
            foreach (MethodInfo method in methods)
            {                    
                method.Invoke(instance, params...);    
            }
    }
难如初 2024-07-28 15:53:48

在我看来,这是问题定义方面的问题,而不是编码方面的问题。

实例方法取决于调用它们的实例 - 调用实例方法而不关心它被调用的内容是没有意义的。 (正如 Martin 所说,不关心调用哪个实例的实例方法几乎应该总是静态的。我能想到的唯一直接例外是虚拟方法,其中实例隐式指定要使用哪个实现。)

找出带注释的实例方法在您的上下文中的真正含义。 为什么你还要尝试调用方法? 更大的图景是什么? 你有什么背景? 我强烈怀疑您需要一些上下文的概念 - 您可以在其上调用实例方法的对象集合。

This strikes me as an issue in terms of the problem definition rather than coding.

Instance methods depend on which instance they're called on - it makes no sense to call an instance method without caring about what it's called on. (As Martin says, an instance method which doesn't care which instance it's being called on should almost always be static. The only immediate exception I can think of for this is virtual methods, where the instance implicitly specifies which implementation to use.)

Work out what it really means in your context for there to be an annotated instance method. Why are you trying to invoke methods anyway? What's the bigger picture? What context do you have? I strongly suspect you'll want some notion of a context - a collection of objects which you can call the instance methods on.

尘世孤行 2024-07-28 15:53:48

实际上,我认为您需要做的是创建现有对象的列表,然后搜索该列表。 因此,当您创建这些可调用对象时,您会将它们存储到列表中(或者列表可能应该是也具有可调用对象的描述的其他类型的对象列表)。 然后,您可以在运行时扫描该列表,找到与您正在处理的事件类型匹配的列表,然后查找其 methodInfo 并调用方法信息上的 .Invoke ,假设这就是您想要执行的操作。 您还需要传递适当的参数,但是您可以通过创建正确类型的对象向量来做到这一点。

Actually, I think what you need to do is to create a list of the existing objects and then search the list. So as you create these invokable objects, you would store them into the list (or perhaps the list should be a list of objects of some other kind that also has a description of the invokable object). Then you can scan the list at runtime, find the one that matches the type of event you are handling, and then look up its methodInfo and call .Invoke on the method info, assuming thats what you want to do. You would also need to pass in the appropriate arguments, but you can do that by creating a vector of objects of the right types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文