相关疑难解决方法(0)

财产隐藏和反思(C#)

在派生类中声明与基类中的属性名称匹配的属性"隐藏"它(除非它使用override关键字覆盖它).Type.GetProperties()如果它们的类型不匹配,则返回基类和派生类属性.但是,如果它们的类型匹配,令人震惊的是仅返回派生类的属性.例如:

class A
{
    protected double p;
    public int P { get { return (int)p; } set { p = value; } }
}
class B : A
{
    public new int P { get { return (int)p; } set { p = value; } }
}
class C : B
{
    public new float P { get { return (float)p; } set { p = value; } }
}
Run Code Online (Sandbox Code Playgroud)

呼叫typeof(C).GetProperties()只会返回BP和CP是否可以以GetProperties() …

.net c# reflection inheritance

21
推荐指数
2
解决办法
5584
查看次数

标签 统计

.net ×1

c# ×1

inheritance ×1

reflection ×1