简体   繁体   English

关于使用反射调用方法的问题

[英]Question on calling a method using reflection

Beautiful sunny day today! 今天美丽的晴天! However, I can't enjoy it because I've been trying to call a dynamic method in Mono for 2 days :-( 但是,我不能享受它,因为我已经尝试在Mono中调用动态方法2天了:-(

The Story: 故事:

I'm trying to call it within a class called 'Template'. 我正在尝试在名为“模板”的类中调用它。 Basically I would love it if I could pass a string to Template and have it run that method, which is defined within the Template class. 基本上,如果我可以将字符串传递给Template并运行该方法(在Template类中定义),则我会很喜欢。 The template class looks like this so far.. 到目前为止,模板类看起来像这样。

namespace Mash
{
    public class Template
    {
        public Template(string methodToCall)
        {
            Type type = this.GetType();
            object ob = Activator.CreateInstance(type);
            object[] arguments = new object[52];
            type.InvokeMember(methodToCall,
                          BindingFlags.InvokeMethod,
                          null,
                          ob,
                          arguments);
        }
        public void methodIWantToCall()
        {
            Console.WriteLine("I'm running the Method!");
        }
    }
}

No errors are received during compile time. 在编译期间没有收到错误。 Once I run it, however, I get 但是,一旦运行它,我就会

'Unhandled Exception: System.MissingMethodException: Method not found: 'Default constructor not found...ctor() of Mash.Template'.' '未处理的异常:System.MissingMethodException:未找到方法:'未找到默认构造函数... Mash.Template的ctor()'。

I think it is failing here: 我认为这里失败了:

object ob = Activator.CreateInstance(type);

If you need any more information please let me know. 如果您需要更多信息,请告诉我。

Thanks in advance!! 提前致谢!!

you don't need another instance of Template if the method you want to call is in the same class.You can use this 你不需要模板的另一个实例,如果你要调用的方法是在同一class.You可以使用

    public class Template
    {        
        public Template(string methodToCall)
        {
              this.GetType().InvokeMember(methodToCall,
                          BindingFlags.InvokeMethod,
                          null,
                          this,
                          null);

        }
        public void methodIWantToCall()
        {
            Console.WriteLine("I'm running the Method!");
        }
   }

I tested it with: 我用以下方法进行了测试:

class Program
{
    public static void Main(string[] args)
    {
        Template m = new Template("methodIWantToCall");
        Console.ReadKey(true);

    }
 }

The first argument of Activator.CreateInstance is the type of the class, and then follows the argument of the constructor of the type. Activator.CreateInstance的第一个参数是类的类型,然后跟随该类型的构造函数的参数。

You're trying to create an instance of the Template class using no parameter for the constructor. 您正在尝试不使用构造函数的参数来创建Template类的实例。 But there isn't a constructor with no parameter. 但是没有没有参数的构造函数。

Try adding a constructor into your Template class, which takes no parameters: 尝试将构造函数添加到您的Template类中,该构造函数不使用任何参数:

public class Template
{
    //......
    public Template()
    {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM