gpt4 book ai didi

c# - 使用包含 Type 的变量创建 Generic 类型实例

转载 作者:IT王子 更新时间:2023-10-29 03:38:38 25 4
gpt4 key购买 nike

是否可以实现如下代码?我知道这行不通,但我想知道是否有解决方法?

Type k = typeof(double);
List<k> lst = new List<k>();

最佳答案

是的,有:

var genericListType = typeof(List<>);
var specificListType = genericListType.MakeGenericType(typeof(double));
var list = Activator.CreateInstance(specificListType);

关于c# - 使用包含 Type 的变量创建 Generic<T> 类型实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2078914/

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