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

发布于 2024-12-01 14:16:31 字数 418 浏览 3 评论 0原文

如何获取带有字符串的静态类的实例?

例子:

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

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

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

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

发布评论

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

评论(5

冬天的雪花 2024-12-08 14:16:31
Type appleType = Type.GetType("Apple");
MethodInfo methodInfo = appleType.GetMethod(
                            "GetInstance",
                            BindingFlags.Public | BindingFlags.Static
                        );
object appleInstance = methodInfo.Invoke(null, null);

请注意,在 Type.GetType 中,您需要使用程序集限定名称

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.

泅渡 2024-12-08 14:16:31

这是一个完整的例子。只需传入要加载的类型的名称和要调用的方法的名称:

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
        }

}

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
        }

}
笔芯 2024-12-08 14:16:31

您可以这样做:

string fruitIWant = "ApplicationName.Apple";

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

对于 ApplicationName,您可以替换声明该类的名称空间。

(经过测试并工作。)

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.)

波浪屿的海角声 2024-12-08 14:16:31

好吧,也许我收到了你的问题。
伪代码

编辑

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;
      }
}

应该可以工作..

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..

暖风昔人 2024-12-08 14:16:31

虽然其他人给了你你所要求的东西,但这可能就是你想要的:

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

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