美文网首页
C#使用Type对象创建泛型类的实例

C#使用Type对象创建泛型类的实例

作者: 段煜华 | 来源:发表于2019-09-29 19:21 被阅读0次

    使用Reflection:

    //创建对象
    Type elType = Type.GetType(T);
    Type genType = typeof(GenericType<>).MakeGenericType(elType);
    object obj = Activator.CreateInstance(genType);
    //调用对象的方法
    MethodInfo method = genType.GetMethod("MethodName",BindingFlags.Instance | BindingFlags.Public);
    method.Invoke(obj,null);
    

    相关文章

      网友评论

          本文标题:C#使用Type对象创建泛型类的实例

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