假设我们有这两个类:
public class Derived : Base
{
public Derived(string s)
: base(s)
{ }
}
public class Base
{
protected Base(string s)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能从内部的构造函数中找出Base那Derived是调用?这就是我想出的:
public class Derived : Base
{
public Derived(string s)
: base(typeof(Derived), s)
{ }
}
public class Base
{
protected Base(Type type, string s)
{
}
}
Run Code Online (Sandbox Code Playgroud)
还有另一种不需要传递的方法typeof(Derived),例如,某种方式在Base构造函数中使用反射吗?