gpt4 book ai didi

c# - 基于 C# 中给定对象类型的动态列表创建

转载 作者:行者123 更新时间:2023-11-30 16:11:18 25 4
gpt4 key购买 nike

我相当确定这是可能的,而且我认为这很容易,我只是不知道在 Google 中提问的正确方式。我想要做的是将 type 传递给方法并返回该类型的对象列表。像这样:

public List<T> GetComponentsOfType(Type thisType)
{
return Components.OfType<thisType>().ToList();
}

当然不能编译,因为我不知道我在做什么。

最佳答案

我猜你想传入类型参数,这样你就可以获得类型列表:

public List<T> GetComponentsOfType<T>()
{
return Components.OfType<T>().ToList();
}

关于c# - 基于 C# 中给定对象类型的动态列表创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24519526/

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