简体   繁体   English

如何从字符串c#中获取自定义类的类型

[英]How to get type of custom class from string c#

I am trying to use GetType method for my custom class .我正在尝试对我的自定义类使用 GetType 方法。 I have the name of the class as string and i want to get type of it dynamically.我将类的名称作为字符串,我想动态获取它的类型。 I have the same name for two different classes which are located in different directories.对于位于不同目录中的两个不同类,我有相同的名称。

For Example:例如:

MyClass.cs in Folder1: Folder1 中的 MyClass.cs:

namespace ConsoleApplication1.Folder1
{
    public class MyClass : IClass
    {
        public void PrintMe()
        {
            System.Console.WriteLine("I am Folder 1 Class");
        }
    }
}

MyClass.cs in Folder2: Folder2 中的 MyClass.cs:

namespace ConsoleApplication1.Folder2
{
    public class MyClass : IClass
    {
        public void PrintMe()
        {
            System.Console.WriteLine("I am Folder 2 Class");
        }
    }
}

Namespace is ConsoleApplication1命名空间是 ConsoleApplication1

different classes with the same name are in the Folder1 and Folder2.具有相同名称的不同类位于 Folder1 和 Folder2 中。

I want to get it's type from such a string:我想从这样的字符串中获取它的类型:

var runtimeString = "Folder1.MyClass" 

There is method mentioned in MSDN named GetType(string fileName)MSDN 中提到了名为 GetType(string fileName) 的方法

How can i get type of the file and resolve it from the serviceLocator with type on runtime like:我如何获取文件类型并从 serviceLocator 使用运行时类型解析它,例如:

var typeOfMyClass = GetType(runtimeString);    
var instanceOfMyClass = ServiceLocator.Resolve<TypeOfMyClass>(); 

You appear to be describing a need for a factory method, something along the lines of:您似乎在描述对工厂方法的需求,大致如下:

public class MyClassFactory : IMyClassFactory
{
    private Dictionary<string, Action<IClass>> _factory =
        new Dictionary<string, Action<IClass>>
        {
            ["Folder1.MyClass"] = () => new ConsoleApplication1.Folder1.MyClass(),
            ["Folder2.MyClass"] = () => new ConsoleApplication1.Folder2.MyClass(),
            ...
        };

    public IClass GetClassInstance(string myClassName)
    {
        if (_factory.Contains(myClassName))
        {
            return _factory[myClassName]();
        }
        throw NoSuchClassException(myClassName);
    }
} 

I believe the following is what you are trying to accomplish:我相信以下是您要完成的任务:

    static void Main(string[] args)
    {
        var runtimeString = "Folder1.MyClass";
        IClass instanceOfMyClass = (IClass)CreateInstance(runtimeString);
        instanceOfMyClass.PrintMe();

        Console.ReadKey();
    }
    private static object CreateInstance(string className)
    {
        var type = Assembly.GetExecutingAssembly().GetTypes()
            .First(t => t.FullName.EndsWith(className));

        return Activator.CreateInstance(type);
    }

You may use Activator.CreateInstance() method to create an object of a class from its name string as below.您可以使用 Activator.CreateInstance() 方法从其名称字符串创建一个类的对象,如下所示。

Create the Type object:创建类型对象:

Type type1 = typeof(MyClass);

or或者

Type type1 = Type.GetType("MyClass");

Create an instance of that type:创建该类型的实例:

Object o = Activator.CreateInstance(type1);

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

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