返回继承类型 class
Returning the type of an inherited class
考虑:
class BasicType
{
public BasicType() { }
public T Save<T>() where T : BasicType
{
BasicType b = DataContext.Save(this); //Returns a BasicType
return (T)Activator.CreateInstance(typeof(T), b);
}
}
class DerivedType : BasicType
{
public DerivedType(BasicType b) { }
}
public static void Main()
{
DerivedType d = new DerivedType();
d = d.Save<DerivedType>();
}
这行得通,但每次我调用 Save 时都被迫指定类型是一个拖累。
有什么方法可以更改 BasicType.Save 方法,使其始终 return 调用 Save 的实例的实际类型(派生或基类)的实例?
像这样。
class BasicType
{
public BasicType()
{
}
protected virtual T Save<T>()
{
BasicType b = DataContext.Save(this); //Returns a BasicType
return (T)Activator.CreateInstance(typeof(T), b);
}
}
class DerivedType : BasicType
{
public DerivedType(BasicType b)
{
}
public DerivedType Save()
{
return base.Save<DerivedType>();
}
}
public static void Main()
{
DerivedType d = new DerivedType(new BasicType());
d = d.Save();
}
您可以更改 BasicType
的定义,以便在继承时强制提供 T
的类型。
像这样:
class BasicType<T> where T : BasicType<T>, new()
{
public BasicType() { }
public T Save()
{
T b = new T();
return (T)Activator.CreateInstance(typeof(T), b);
}
}
class DerivedType : BasicType<DerivedType>
{
public DerivedType() { }
}
class Program
{
static void Main(string[] args)
{
DerivedType d = new DerivedType();
d = d.Save();
}
}
在这种情况下不需要泛型。
我认为这应该足够了:
public BasicType Save()
{
BasicType b = DataContext.Save(this); //Returns a BasicType
return (BasicType)Activator.CreateInstance(this.GetType(), b);
}
无论如何你应该小心,因为继承的 类 可能没有预期的构造函数。
最好覆盖保存方法,或者至少覆盖特定部分。
考虑:
class BasicType
{
public BasicType() { }
public T Save<T>() where T : BasicType
{
BasicType b = DataContext.Save(this); //Returns a BasicType
return (T)Activator.CreateInstance(typeof(T), b);
}
}
class DerivedType : BasicType
{
public DerivedType(BasicType b) { }
}
public static void Main()
{
DerivedType d = new DerivedType();
d = d.Save<DerivedType>();
}
这行得通,但每次我调用 Save 时都被迫指定类型是一个拖累。 有什么方法可以更改 BasicType.Save 方法,使其始终 return 调用 Save 的实例的实际类型(派生或基类)的实例?
像这样。
class BasicType
{
public BasicType()
{
}
protected virtual T Save<T>()
{
BasicType b = DataContext.Save(this); //Returns a BasicType
return (T)Activator.CreateInstance(typeof(T), b);
}
}
class DerivedType : BasicType
{
public DerivedType(BasicType b)
{
}
public DerivedType Save()
{
return base.Save<DerivedType>();
}
}
public static void Main()
{
DerivedType d = new DerivedType(new BasicType());
d = d.Save();
}
您可以更改 BasicType
的定义,以便在继承时强制提供 T
的类型。
像这样:
class BasicType<T> where T : BasicType<T>, new()
{
public BasicType() { }
public T Save()
{
T b = new T();
return (T)Activator.CreateInstance(typeof(T), b);
}
}
class DerivedType : BasicType<DerivedType>
{
public DerivedType() { }
}
class Program
{
static void Main(string[] args)
{
DerivedType d = new DerivedType();
d = d.Save();
}
}
在这种情况下不需要泛型。
我认为这应该足够了:
public BasicType Save()
{
BasicType b = DataContext.Save(this); //Returns a BasicType
return (BasicType)Activator.CreateInstance(this.GetType(), b);
}
无论如何你应该小心,因为继承的 类 可能没有预期的构造函数。
最好覆盖保存方法,或者至少覆盖特定部分。