class a
{
a()
{
Type type = ...//子类的Type
}
}
class b: a
{
}class demo()
{
static int Main()
{ }
}-----------------------------------------
当然不能使用:
Type type = typeof(B);
因为我写a不知道会有b存在..
{
a()
{
Type type = ...//子类的Type
}
}
class b: a
{
}class demo()
{
static int Main()
{ }
}-----------------------------------------
当然不能使用:
Type type = typeof(B);
因为我写a不知道会有b存在..
{
Type type = typeof(b);
}否则应该考虑多态
------------------------
那就是说,此路不通?
但每次都要在子类里写相同的代码总是不爽!
class a
{
protected Type type;
a()
{
}
private void GetChildrenFields()
{
FieldInfo[] fields = type.GetFields();
....
}
}
class b: a
{
type = this.GetType(); // 这段代码每个子类都得写
}
{
private Type type;
a()
{
}
protected virtual void GetChildrenFields()
{
type = this.GetType(); //搞定,读出来的就是子类的类型
FieldInfo[] fields = type.GetFields();
....
}
}
class b: a
{
}