如何获取class的所有泛型参数?
How to get all generic parameters of class?
我有一个摘要 class,其中包含一些或许多(从 1 个一直到 16 个)通用参数,如下所示:
public abstract class ComponentSystem<F1> : ComponentSystemBase
where F1 : IComponent
{ }
public abstract class ComponentSystem<F1, F2> : ComponentSystemBase
where F1 : IComponent
where F2 : IComponent
{ }
public abstract class ComponentSystem<F1, F2, F3> : ComponentSystemBase
where F1 : IComponent
where F2 : IComponent
where F3 : IComponent
{ }
public abstract class ComponentSystem<F1, F2, F3, F4> : ComponentSystemBase
where F1 : IComponent
where F2 : IComponent
where F3 : IComponent
where F4 : IComponent
{ }
现在我有一个基础 class,其中包含如下类型列表:
public abstract class ComponentSystemBase
{
public List<Type> Filters { get; private set; }
public abstract void Init();
public abstract void Update();
protected internal void PopulateFilterList(Type[] filters)
{
// oneliner?
foreach (Type filter in filters)
{
Filters.Add(filter);
}
}
}
现在我的问题是,如何获取类型数组中的所有泛型参数,以便将其传递给将其添加到列表中的函数?
是否有获取所有通用参数的函数,我是否必须为每个 class 手动执行?
如果有什么不清楚的地方,请告诉我,我会澄清的!
反思救援!
Type[] types = GetType().GetGenericArguments();
我有一个摘要 class,其中包含一些或许多(从 1 个一直到 16 个)通用参数,如下所示:
public abstract class ComponentSystem<F1> : ComponentSystemBase
where F1 : IComponent
{ }
public abstract class ComponentSystem<F1, F2> : ComponentSystemBase
where F1 : IComponent
where F2 : IComponent
{ }
public abstract class ComponentSystem<F1, F2, F3> : ComponentSystemBase
where F1 : IComponent
where F2 : IComponent
where F3 : IComponent
{ }
public abstract class ComponentSystem<F1, F2, F3, F4> : ComponentSystemBase
where F1 : IComponent
where F2 : IComponent
where F3 : IComponent
where F4 : IComponent
{ }
现在我有一个基础 class,其中包含如下类型列表:
public abstract class ComponentSystemBase
{
public List<Type> Filters { get; private set; }
public abstract void Init();
public abstract void Update();
protected internal void PopulateFilterList(Type[] filters)
{
// oneliner?
foreach (Type filter in filters)
{
Filters.Add(filter);
}
}
}
现在我的问题是,如何获取类型数组中的所有泛型参数,以便将其传递给将其添加到列表中的函数?
是否有获取所有通用参数的函数,我是否必须为每个 class 手动执行?
如果有什么不清楚的地方,请告诉我,我会澄清的!
反思救援!
Type[] types = GetType().GetGenericArguments();