C# 4.0 将字符串数据转换为列表的类型<&燃气轮机;
我想获取一个字符串并根据 “类型”记录在字符串中。例如,假设str是“System.string”。我想要 喜欢为我创建列表的方法。当然,字符串可以包含文本 “引用”程序集中的任何对象。我尝试了以下方法,但未成功:C# 4.0 将字符串数据转换为列表的类型<&燃气轮机;,c#-4.0,C# 4.0,我想获取一个字符串并根据 “类型”记录在字符串中。例如,假设str是“System.string”。我想要 喜欢为我创建列表的方法。当然,字符串可以包含文本 “引用”程序集中的任何对象。我尝试了以下方法,但未成功: Type classType = Type.GetType(str); List<classType> wgList = new List<classType>(); Type classType=Type.GetType(str); Lis
Type classType = Type.GetType(str);
List<classType> wgList = new List<classType>();
Type classType=Type.GetType(str);
List wgList=新列表();
我收到一条消息,说明“classType是一个字段,但被用作一个类型”
我该怎么处理才能得到我需要的?
以下代码提供了一个很好的解决方案:
Type ty = Type.GetType(ItemType);
wgList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(ty));
Type ty=Type.GetType(ItemType);
wgList=(IList)Activator.CreateInstance(typeof(List).MakeGenericType(ty));
你不能那样做,或者我不明白你的目的是什么
改用List