gpt4 book ai didi

c# - 使用 GetType() 创建 List

转载 作者:行者123 更新时间:2023-11-30 13:52:00 26 4
gpt4 key购买 nike

是否可以使用 GetType() 创建 List 或 IEnumerable。

// If T is Type of Contact I want to Return List<Contact>

Test(typeof(Contact));//return List<Type>

public static IEnumerable<T> Test<T>(T t)
{
return new List<T>(); //return List<Type>

}

最佳答案

 public static IList GetList(Type type)
{
return (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
}

关于c# - 使用 GetType() 创建 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3454382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com