我的基类使用对派生类的反射来为它们提供一些功能。目前我这样做:如何在没有泛型的基类中获得派生类的类型?
abstract class BaseClass<T>
{
string GetClassString()
{
// Iterate through derived class, and play with it's properties
// So I need to know the type of derived class (here T).
return result;
}
static bool TryParse(string classString, out T result)
{
// I should declare a variable as T and return it
}
}
我可以做到这一点没有泛型?
'T' *不是*派生类的类型 - 或者至少不必是。想象一下'class MyDerivedClass:BaseClass'。这里派生类是'MyDerivedClass',但'T'是'string'。 –
2011-03-01 12:47:30
@Jon:那么如何声明'TryParse(string classString,out T derivedType) '? 'C#'是否允许我动态地在方法签名中定义'derivedType'并且不使用'T'? – Xaqron 2011-03-02 02:46:52