假设我们有这两个类:
public class Derived : Base
{
public Derived(string s)
: base(s)
{ }
}
public class Base
{
protected Base(string s)
{
}
}
如何在Base
构造函数内部确定是Derived
调用了它?这是我想到的:
public class Derived : Base
{
public Derived(string s)
: base(typeof(Derived), s)
{ }
}
public class Base
{
protected Base(Type type, string s)
{
}
}
有没有其他方法不需要传递 typeof(Derived)
,比如从 Base
的构造函数中使用反射的方式?
Derived1 d1 = new Base();
的输出结果。 - Seph