【发布时间】:2013-04-02 15:30:13
【问题描述】:
我有一个带有静态方法的泛型类,该方法使用类型参数:
GenericClass<T>
{
public static void Method()
{
//takes info from typeof(T)
}
}
现在,我需要访问该静态方法,而不是简单地使用GenericClass<KnownType>.Method()。我需要有一个 Type 实例来做到这一点。所以:
public void OutsiderMethod(Type T)
{
GenericClass<T>.Method()
//it's clear this line won't compile, for T is a Type instance
//but i want some way to have access to that static method.
}
使用反射,我可能可以使用一些 MethodInfo 的东西通过它的字符串名称来调用该方法。 这部分好,解决了问题。 但如果可能的话,我希望不必将名称用作字符串。
有人吗???
【问题讨论】:
-
这种情况下扩展方法不是更合适吗?
-
这需要一个 GenericClass
的实例,T 由 Type 实例给出。
标签: c# generics reflection types static