开发者

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

开发者 https://www.devze.com 2023-03-31 00:31 出处:网络
How can I get an instance of a static class with a string? Example: class Apple : IFruit { public static Apple GetInstance() { ... }

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();


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.


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
        }

}


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.

(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();
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消