相关疑难解决方法(0)

从C#中的基类,得到派生类型?

假设我们有这两个类:

public class Derived : Base
{
    public Derived(string s)
        : base(s)
    { }
}

public class Base
{
    protected Base(string s)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能从内部的构造函数中找出BaseDerived是调用?这就是我想出的:

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构造函数中使用反射吗?

c# reflection inheritance types

57
推荐指数
2
解决办法
4万
查看次数

标签 统计

c# ×1

inheritance ×1

reflection ×1

types ×1