C# 如何使用类型为的变量作为类型?

C# 如何使用类型为的变量作为类型?,c#,C#,我有一个foreach循环运行在类型列表上。将类型为的变量作为类型传入将彻底失败。基于在这个网站上看到的类似问题,我写了一个反射驱动的解决方案,但在运行时失败了。我不知道如何从这一点出发 foreach(在GenDefDatabase.AllDefTypesWithDatabases()中键入defType) { //编译错误:“defType”是一个变量,但与类型一样使用 DefDatabase.ResolveAllReferences(); //运行时错误:System.Argumen

我有一个foreach循环运行在类型列表上。将类型为的变量作为类型传入将彻底失败。基于在这个网站上看到的类似问题,我写了一个反射驱动的解决方案,但在运行时失败了。我不知道如何从这一点出发

foreach(在GenDefDatabase.AllDefTypesWithDatabases()中键入defType)
{   
//编译错误:“defType”是一个变量,但与类型一样使用
DefDatabase.ResolveAllReferences();
//运行时错误:System.ArgumentException:该方法有0个泛型参数,但提供了1个泛型参数。
//在:0中的System.Reflection.MonoMethod.MakeGenericMethod(System.Type[]methodInstantiation)[0x00000]处
typeof(DefDatabase).GetMethod(“ResolveAllReferences”,BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(defType).Invoke(null,null);
}
在本声明中:

typeof(DefDatabase<>).GetMethod("ResolveAllReferences",
    BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(defType).Invoke(null, null);

@just my name可能重复:问题是他们试图调用泛型方法,即使该方法实际上不是泛型的。请看下面我的答案。@PeterDuniho我明白了,类型本身是泛型的,谢谢你指出这一点。
typeof(DefDatabase<>).MakeGenericType(defType).GetMethod("ResolveAllReferences",
    BindingFlags.Public | BindingFlags.Static).Invoke(null, null);