我的基類使用對派生類的反射來爲它們提供一些功能。目前我這樣做:如何在沒有泛型的基類中獲得派生類的類型?
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