Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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# 如何将一种类型的泛型列表强制转换为未知类型的泛型列表_C#_Generics_Reflection - Fatal编程技术网

C# 如何将一种类型的泛型列表强制转换为未知类型的泛型列表

C# 如何将一种类型的泛型列表强制转换为未知类型的泛型列表,c#,generics,reflection,C#,Generics,Reflection,我想编写一个函数,它接受一个列表,并返回一个原始列表,该列表被转换为指定对象类型的列表列表,知道原始列表中的对象类型为对象类型。诀窍在于,ObjectType可以是任何类型,我使用反射找到它。很抱歉缺少代码,但我甚至不知道如何执行此操作。如果您知道列表中的每个项目都属于ObjectType类型,则可以执行以下操作: List<object> sourceList = new List<object>() { 1, 2, 3 }; List<int> resul

我想编写一个函数,它接受一个
列表
,并返回一个原始列表,该列表被转换为指定对象类型的列表
列表
,知道原始列表中的对象类型为
对象类型
。诀窍在于,
ObjectType
可以是任何类型,我使用反射找到它。很抱歉缺少代码,但我甚至不知道如何执行此操作。

如果您知道列表中的每个项目都属于
ObjectType
类型,则可以执行以下操作:

List<object> sourceList = new List<object>() { 1, 2, 3 };
List<int> resultList = sourceList.Cast<int>().ToList();
List sourceList=newlist(){1,2,3};
List resultList=sourceList.Cast().ToList();
如果您确实希望以通用方式转换列表中的每个项目,最简单的方法是执行以下操作:

public static IEnumerable<T> ConvertTo<T>(this IEnumerable items)
{
    return items.Cast<object>().Select(x => (T)Convert.ChangeType(x, typeof(T)));
}
公共静态IEnumerable ConvertTo(此IEnumerable项)
{
returnitems.Cast().Select(x=>(T)Convert.ChangeType(x,typeof(T));
}
这将作为扩展方法实现,因此您可以编写:

List<object> sourceList = new List<object>() { 1, 2, 3 };
List<string> resultList = sourceList.ConvertTo<string>().ToList();
List sourceList=newlist(){1,2,3};
List resultList=sourceList.ConvertTo().ToList();
如果目标类型在编译时未知,则确实需要使用反射。类似这样的方法会奏效:

class ListUtil
{
    public static List<T> ConvertToList<T>(this IEnumerable items)
    {
        // see method above
        return items.ConvertTo<T>().ToList();
    }

    public static IList ConvertToList(this IEnumerable items, Type targetType)
    {
        var method = typeof(ListUtil).GetMethod(
            "ConvertToList", 
            new[] { typeof(IEnumerable) });
        var generic = method.MakeGenericMethod(targetType);
        return (IList)generic.Invoke(null, new[] { items });
    }
}
类ListUtil
{
公共静态列表转换器列表(此IEnumerable items)
{
//见上述方法
返回项目.ConvertTo().ToList();
}
公共静态IList转换器列表(此IEnumerable items,类型targetType)
{
var method=typeof(ListUtil).GetMethod(
“兑换者”,
新[]{typeof(IEnumerable)});
var generic=method.MakeGenericMethod(targetType);
return(IList)generic.Invoke(null,new[]{items});
}
}
现在你可以这样称呼它:

List<object> sourceList = new List<object>() { 1, 2, 3 };
IList resultList = ListUtil.ConvertToList(sourceList, typeof(string));
resultList.GetType(); // List<string>
List sourceList=newlist(){1,2,3};
IList resultList=ListUtil.ConvertToList(sourceList,typeof(string));
resultList.GetType();//列表

当然,使用此方法会丢失任何编译时类型安全性。

为什么不
返回items.Cast().ToList()
?@bm仅当所有对象的类型为
T
时使用。它基本上是一个盒子/联合国盒子。在我使用整数作为输入的示例中,
.Cast
.Cast
将失败。如果该类型仅在运行时已知,则上述方法不会失败work@p.s.w.g,啊,我看到+1了。@BrokenGlass看到了我更新后的答案,这是一个只有在运行时才知道类型的解决方案。