Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 将字符串数据转换为列表的类型<&燃气轮机;_C# 4.0 - Fatal编程技术网

C# 4.0 将字符串数据转换为列表的类型<&燃气轮机;

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

我想获取一个字符串并根据 “类型”记录在字符串中。例如,假设str是“System.string”。我想要 喜欢为我创建列表的方法。当然,字符串可以包含文本 “引用”程序集中的任何对象。我尝试了以下方法,但未成功:

    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

您可以使用System.Linq命名空间中的扩展方法is Cast()将列表转换为列表

List<Entry> entries = wgList.Cast<Entry>().ToList()
List entries=wgList.Cast().ToList()

我正在尝试根据字符串内容创建强类型列表。我使用以下方法提供了所需内容: