C#动态创建实例化泛型对象,实例化新对象 new()

普通类实例化:

Assembly assembly = Assembly.Load("Dsh.Data");
Type type = assembly.GetType("Dsh.Data.SqlServer");
Interface.IDal Dal = (Interface.IDal)Activator.CreateInstance(type, true);

泛型类实例化:(注意`1

Assembly assembly = Assembly.Load("Dsh.Data");
Type type = assembly.GetType("Dsh.Data.SqlServer" + "`1").MakeGenericType(typeof(TEntity));
Interface.IDal Dal = (Interface.IDal)Activator.CreateInstance(type, true);

泛型类(多个泛型)实例化:(注意`2

Assembly assembly = Assembly.Load("Dsh.Data");
Type type = assembly.GetType("Dsh.Data.SqlServer" + "`2").MakeGenericType(typeof(TEntity), typeof(TPrimaryKey));
Interface.IDal = (Interface.IDal)Activator.CreateInstance(type, true);

你可能感兴趣的:(C#动态创建实例化泛型对象,实例化新对象 new())