【发布时间】:2011-07-06 11:52:03
【问题描述】:
我的基类使用派生类的反射来为它们提供一些功能。目前我是这样做的:
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<string>。这里派生类是MyDerivedClass,但T是string。 -
@Jon: 那么如何声明
TryParse(string classString, out T derivedType)呢?C#是否让我在方法签名中动态定义derivedType而不要使用T?
标签: c# generics types base-class