C#运行时通过字符串实例化类对象(通过类名字符串实例化对象)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
 
namespace CS_Test
{
    public class BaseBullet
    {
        public float x;
        public float y;
        public int color;
        public string name;
 
        public BaseBullet(string name, int color)
        {
            this.name = name;
            this.color = color;
        }
 
        public virtual void say(string str)
        {
            Console.WriteLine(str);
            Console.WriteLine("Name is " + name);
            Console.WriteLine("Color is " + color);
        }
    }
    public class BulletType1 : BaseBullet
    {
        public BulletType1(string name, int color) : base(name, color)
        {
 
        }
 
        public override void say(string str)
        {
            base.say(str);
            Console.WriteLine("shoot from BulletType1");
        }
 
    }
    public class BulletType2 : BaseBullet
    {
        public BulletType2(string name, int color) : base(name, color)
        {
        }
        public override void say(string str)
        {
            base.say(str);
            Console.WriteLine("shoot from BulletType2");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            int id = 1;
            string className = "CS_Test.BulletType" + id.ToString();
            System.Type t = System.Type.GetType(className);
            BaseBullet b1 = Activator.CreateInstance(t, "John", 1) as BaseBullet;
            b1.say("Hello");
 
            ++id;
            className = "CS_Test.BulletType" + id.ToString();
 
            t = System.Type.GetType(className);
            BaseBullet b2 = Activator.CreateInstance(t, "Sam", 2) as BaseBullet;
            b2.say("Goodbye");
        }
    }
}

========================

LoaderBase loader = Activator.CreateInstance(Type.GetType(type,true)) as LoaderBase;

Activator 类
包含用于在本地创建对象类型的方法。 无法继承此类。

简单说 Activator.CreateInstance :使用与指定参数匹配程度最高的构造函数来创建指定类型的实例。

使用Activator.CreateInstance 的实际作用是什么呢?

是因为 想创建一个方法 方法中传入一个类的名称 然后就能返回一个这个类的实例 ,这样的做法让程序有更高的拓展性,

下面附上 项目中使用的例子

public static OperatorAllLoader OpeatorAllLoader = New("OperatorAllLoader") as OperatorAllLoader;
 private static List<LoaderBase> InfoList = new List<LoaderBase>();
    private static LoaderBase New(string type)
    {    
        // ReSharper disable once AssignNullToNotNullAttribute
        LoaderBase loader = Activator.CreateInstance(Type.GetType(type,true)) as LoaderBase;
        InfoList.Add(loader);
        InfoDict.Add(type, loader);
        return InfoList[InfoList.Count - 1];
    }

还有一个网上的例子

using System;
 
namespace ActivatorCreateInstance
{
 /// <summary>
 /// IObjcet 的摘要说明。
 /// </summary>
 public interface IObjcet
 {
  void printName();
 }
}
 
 
//实现接口的类:比如在上例中,是交通工具类,这种类可以在扩展的时候添加,其它类的代码不用修改
 
using System;
 
namespace ActivatorCreateInstance
{
 /// <summary>
 /// ClassExam 的摘要说明。
 /// </summary>
 public class ClassExam:IObjcet
 {
  private string name="default name";
  public ClassExam()
  {
   
  }
 
  public ClassExam(string name)
  {
   this.name =name;
  }
 
  public void printName()
  {
   Console .WriteLine (this.ToString ()+"'s name is:");
  Console .WriteLine (this.name );
 
  }
  }
}
 
 
 
//程序入口:
 
 
namespace ActivatorCreateInstance
{
 /// <summary>
 /// main 的摘要说明。
 /// </summary>
 public class main
 {
  public main()
  {
   
  }
 
  public static void Main()
  {
 
//用传递参数来得到一个类的实例
 
 
   //用Activator .CreateInstance创建函数实例,默认的不带参数的构造函数
   IObjcet obj=(IObjcet)Activator .CreateInstance(System.Type .GetType ("ActivatorCreateInstance.ClassExam,ActivatorExample" ),null);
   //System.Type .GetType  命名空间.类名,程序集
   obj.printName();
 
   //调用ClassExam类的另一个带参数构造函数
   IObjcet obj2=(IObjcet)System.Activator .CreateInstance (System.Type .GetType ("ActivatorCreateInstance.ClassExam,ActivatorExample" ),new string []{"seted new name"});
   obj2.printName ();
  }
 }
}

转: https://blog.csdn.net/weixin_33910759/article/details/93950393

https://blog.csdn.net/ldy597321444/article/details/78362145