C# 通过 MethodInfo 调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/919826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoke method by MethodInfo
提问by apparat
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.
我想调用具有特定属性的方法。所以我循环遍历所有程序集和所有方法以查找具有我的属性的方法。工作正常,但是当我只获得 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.
问题是我不知道包含该特定方法的类的实例。所以我无法正确调用它,因为这些方法不是静态的。如果可能,我还想避免创建此类的新实例。
采纳答案by Jon Skeet
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.)
实例方法取决于调用它们的实例 - 调用实例方法而不关心它被调用的内容是没有意义的。(正如 Martin 所说,一个不关心它被调用的实例的实例方法应该几乎总是静态的。我能想到的唯一直接例外是虚拟方法,其中实例隐式指定要使用的实现。)
Work out what it really meansin 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 cancall the instance methods on.
弄清楚在你的上下文中,有一个带注释的实例方法到底意味着什么。你为什么要尝试调用方法?更大的图景是什么?你有什么背景?我强烈怀疑您会想要一些上下文的概念 - 您可以调用实例方法的对象集合。
回答by Martin Harris
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...);
}
}
回答by Ken Birman
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.
实际上,我认为您需要做的是创建一个现有对象的列表,然后搜索该列表。因此,当您创建这些可调用对象时,您会将它们存储到列表中(或者该列表应该是其他类型的对象列表,并且还具有可调用对象的描述)。然后您可以在运行时扫描列表,找到与您正在处理的事件类型相匹配的列表,然后查找其 methodInfo 并在方法信息上调用 .Invoke ,假设这就是您想要做的。您还需要传入适当的参数,但您可以通过创建正确类型的对象向量来实现。