美文网首页
C#动态创建实例化泛型对象,实例化新对象 new()

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

作者: 段煜华 | 来源:发表于2019-09-29 19:20 被阅读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);
    

    相关文章

      网友评论

          本文标题:C#动态创建实例化泛型对象,实例化新对象 new()

          本文链接:https://www.haomeiwen.com/subject/ldmbyctx.html