C#动态创建实例化泛型对象,实例化新对象 new()
2019-09-29 本文已影响0人
段煜华
普通类实例化:
Assembly assembly = Assembly.Load("Dsh.Data");
Type type = assembly.GetType("Dsh.Data.SqlServer");
Interface.IDal<TEntity> Dal = (Interface.IDal<TEntity>)Activator.CreateInstance(type, true);
泛型类实例化:(注意`1
)
Assembly assembly = Assembly.Load("Dsh.Data");
Type type = assembly.GetType("Dsh.Data.SqlServer" + "`1").MakeGenericType(typeof(TEntity));
Interface.IDal<TEntity> Dal = (Interface.IDal<TEntity>)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<TEntity, TPrimaryKey> = (Interface.IDal<TEntity, TPrimaryKey>)Activator.CreateInstance(type, true);