码迷,mamicode.com
首页 > 其他好文 > 详细

动态传入泛型类

时间:2018-07-03 20:08:42      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:gen   pre   activator   不可   eric   vat   运行时   ict   als   

有这样的场景,对于泛型参数T需要在运行时传入,如下面的T

public class AnimalContext<T>
{
    public DoAnimalStuff()
    {
        //AnimalType Specific Code
    }
}

用一般方式是不可行的,因为T是在编译时确定的。

可以用反射来实现。

var type = typeof(AnimalContext<>).MakeGenericType(typeA.GetType());
var typeA_Context = Activator.CreateInstance(type);

 

动态传入泛型类

标签:gen   pre   activator   不可   eric   vat   运行时   ict   als   

原文地址:https://www.cnblogs.com/luhe/p/9259918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!