简体   繁体   English

获取对类的引用并仅从字符串值执行静态方法?

[英]Get reference to a class and execute static method from only a string value?

How can I get an instance of a static class with a string? 如何获取带字符串的静态类的实例?

Example: 例:

class Apple : IFruit
{
    public static Apple GetInstance() { ... }
    private Apple() { }

    // other stuff
}

class Banana : IFruit
{
    public static Banana GetInstance() { ... }
    private Banana() { }

    // other stuff
}

// Elsewhere in the code...
string fruitIWant = "Apple";
IFruit myFruitInstance = [What goes here using "fruitIWant"?].GetInstance();

Here is a complete example. 这是一个完整的例子。 Just pass in the name of the type you want to load and the name of the method to invoke: 只需传入要加载的类型的名称以及要调用的方法的名称:

namespace Test
{

    class Program
    {
        const string format = @"hh\:mm\:ss\,fff";
        static void Main(string[] args)
        {
            Console.WriteLine(Invoke("Test.Apple", "GetInstance"));
            Console.WriteLine(Invoke("Test.Banana", "GetInstance"));
        }
        public static object Invoke(string type, string method)
        {
            Type t = Type.GetType(type);
            object o = t.InvokeMember(method, BindingFlags.InvokeMethod, null, t, new object[0]);
            return o;
        }

        }
        class Apple 
        {
            public static Apple GetInstance() { return new Apple(); }
            private Apple() { }

            // other stuff
        }

        class Banana
        {
            public static Banana GetInstance() { return new Banana(); }
            private Banana() { }

            // other stuff
        }

}
Type appleType = Type.GetType("Apple");
MethodInfo methodInfo = appleType.GetMethod(
                            "GetInstance",
                            BindingFlags.Public | BindingFlags.Static
                        );
object appleInstance = methodInfo.Invoke(null, null);

Note that in Type.GetType you need to use the assembly-qualified name . 请注意,在Type.GetType您需要使用程序集限定名称

You do like this: 你喜欢这样:

string fruitIWant = "ApplicationName.Apple";

IFruit a = Type.GetType(fruitIWant).GetMethod("GetInstance").Invoke(null, null) as IFruit;

For ApplicationName you substitute the namespace where the class is declared. 对于ApplicationName您可以替换声明类的名称空间。

(Tested and working.) (经过测试和工作。)

OK, may be I got your question. 好的,可能是我收到了你的问题。 A pseudocode 伪代码

EDIT 编辑

foreach (var type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
{
      if (type.Name.Equals("MyClass"))
      {
          MethodInfo mi = type.GetMethod("GetInstance", BindingFlags.Static);
          object o = mi.Invoke(t, null);
          break;
      }
}

Should work.. 应该管用..

While the others give you what you asked for, this is probably what you want: 虽然其他人给你你所要求的,这可能是你想要的:

IFriut GetFruit(string fruitName)
{
   switch(fruitName)
   {
       case "Apple":
          return Apple.GetInstance();
       case "Banana":
          return Banana.GetInstance();
       default:
          throw new ArgumentOutOfRangeException();
   }
}

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

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