Get class by string value

前端 未结 5 903
感动是毒
感动是毒 2021-02-18 14:11

I have an abstract class with a few derived class

public abstract class MyObject
{
    public string name { get; set; }
    public bool IsObject(string pattern);         


        
5条回答
  •  逝去的感伤
    2021-02-18 14:38

    It can be done using Reflection like this...

        public object getInstance(string assemblyName, string className, object[] constructorParameters)
        {
            System.Reflection.Assembly asm = System.Reflection.Assembly.Load(assemblyName);
            return asm.CreateInstance(className, false, System.Reflection.BindingFlags.CreateInstance, null, constructorParameters, null, null);
        }
    

    assemblyName - full path + assembly name

    className - fully qualified class name

提交回复
热议问题