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;

 

 

相关文章:

  • 2022-03-05
  • 2021-09-07
  • 2022-12-23
  • 2021-08-10
  • 2022-02-10
  • 2021-11-21
猜你喜欢
  • 2021-06-23
  • 2022-02-27
  • 2022-12-23
  • 2021-06-02
  • 2021-05-25
  • 2021-10-26
  • 2021-06-25
相关资源
相似解决方案