您的位置:首页 > 其它

反射创建对象,创建泛型集合,创建泛型字典

2010-12-22 17:14 519 查看
object obj = new object();

//反射创建普通对象
object o = Activator.CreateInstance(obj.GetType());

//反射创建泛型集合
Type generic = typeof(List<>);
Type[] typeArgs1 = { obj.GetType() };
generic=generic.MakeGenericType(typeArgs1);
var list=Activator.CreateInstance (generic) as IList;

//反射创建泛型字典
generic = typeof(Dictionary<,>);
Type[] typeArgs2 = { typeof(string), obj.GetType() };
generic = generic.MakeGenericType(typeArgs2);
var dic = Activator.CreateInstance(generic) as IDictionary;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐