这是一个由两部分组成的问题。
首先
我有一个名为 ComponentList 的类,如下所示:
public class ComponentList<T> : List<T> where T : Component
{
}
现在我想创建一个无类型 ComponentLists 的空列表:
List<ComponentList<>> MasterList = new List<ComponentList<>>();
我得到一个错误,因为 ComponentList 需要一个指定的通用类型,即使我还没有尝试初始化任何 ComponentList(只是包含它们的 MasterList)。我如何在不初始化任何 ComponentLists 的情况下声明 ComponentLists 的 MasterList(因为我计划在运行时使用仅在运行时已知的类型初始化它们)?毕竟,MasterList 需要包含不同泛型类型的 ComponentList,而不仅仅是一个。
其次
我知道之前有人问过这个问题,但我似乎无法理解任何提议的解决方案的概念。
如你所知,我有一个 List<>
称为 MasterList,它是 ComponentLists(自定义类)的列表。 ComponentList 是未定义类型的通用类(被限制为 Component 的子类型)。
在下面的示例中,我尝试检查 ComponentList 泛型类型(从 MasterList 引用)是否与此类(代码从中调用的类)相同。
if (MasterList[i].GetType() == typeof(ComponentList<this.GetType()>))
{
}
问题是,代码应该从未知的子类中自动调用,而不是从这个父类中调用。所以ComponentList泛型需要和子类的类型比较,而不是这个基类。因此,“this.GetType”代替了实际基类的硬编码名称。
this.GetType()
ComponentList<> 中传递的类型返回“预期类型”错误,显然是因为 GetType()
返回编译时类型,而不是运行时类型(这是我需要的)。
那么我怎样才能得到运行时类型呢?如果我做不到,什么是完成我想做的事情的最佳选择? (我听说过一些叫做反射的东西,可能对我有帮助,但我真的不明白)。
最佳答案
您无法在运行时确定通用类型。 C#(实际上都是 .Net)是故意这样设计的。编译器对待泛型类型的方式几乎与对待显式定义类型的方式相同。
考虑以下几点:
class MyGenericClass<T>
{
public static T MyProperty { get; set; }
}
class MyIntClass
{
public static int MyProperty { get; set; }
}
class UseThem
{
public void test()
{
// both are JIT'ed to be exactly the same machine code
var foo = MyGenericClass<int>.MyProperty;
var bar = MyOtherClass.MyProperty;
}
}
因此,您必须为泛型类提供一个类型,以便 JIT 知道要编译什么。
可能的选择 如果所有可能最终成为泛型类型的可能类型都是相似的(继承自相同的基类或实现相似的接口(interface)),那么您可以使用接口(interface)或基类实例化泛型类作为泛型类型:
List<ComponentList<ComponentBase>> MasterList = new List<ComponentList<ComponentBase>>();
// -OR-
List<ComponentList<IMyComponent>> MasterList = new List<ComponentList<IMyComponent>>();
我有一种预感,您应该能够通过一些创造性的重构来定义一个通用接口(interface)或基类。 :)
关于c# - 我如何声明具有不同类型和未知类型的泛型列表以及如何使用仅在运行时已知的类型初始化泛型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16351091/