Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关于继承静态属性的思考_C#_.net_C# 4.0_Reflection_Properties - Fatal编程技术网

C# 关于继承静态属性的思考

C# 关于继承静态属性的思考,c#,.net,c#-4.0,reflection,properties,C#,.net,C# 4.0,Reflection,Properties,在.NET4.0中,通过反射获取静态属性时遇到一些问题 例如,假设我有以下课程: class Foo { public static int MyProperty { get { return 1234; } } } class Bar : Foo { } 现在如果我打电话: typeof(Foo).GetProperties(); 我得到了一个属性“MyProperty”的预期列表。如果我打电话: typeof(Bar).GetPropertie

在.NET4.0中,通过反射获取静态属性时遇到一些问题

例如,假设我有以下课程:

class Foo
{
    public static int MyProperty
    {
        get { return 1234; }
    }
}

class Bar : Foo
{

}
现在如果我打电话:

typeof(Foo).GetProperties();
我得到了一个属性“MyProperty”的预期列表。如果我打电话:

typeof(Bar).GetProperties();
我什么也得不到。不幸的是,我严格处理存储为
Type
数据类型的值,因此无法直接调用
typeof(Foo).GetProperties()

谢谢你的帮助

godwin

您可以指定
BindingFlags.FlatterHierarchy
以获取基类中声明的静态属性:

var props = typeof(Bar).GetProperties(BindingFlags.Public |
                                      BindingFlags.Static | 
                                      BindingFlags.FlattenHierarchy);
如果需要私有属性,则需要添加
BindingFlags.NonPublic
。看

var prop = typeof (Bar).GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static);