我如何确定,如果财产被重写? [英] How do I determine if a property was overriden?
问题描述
您好我在做一个项目,我需要注册系统,因为所有属性,如此巨大的它需要大量的工作来注册的一切,我希望依赖于XAML中的目的性。
Hello I am doing a project that where I need to register all the properties, because of the system being so huge it would require a lot of work to register all the properties that i want to be dependent for the purpose of Xaml.
的目标是找到所有都在树的顶部属性
The goal is to find all properties that are on the top of the tree.
所以基本上
public class A{
public int Property1 { get; set; }
}
public class B : A{
public int Property2 { get; set; }
public virtual int Property3 { get; set; }
}
public class C : B{
public override int Property3 { get; set; }
public int Property4 { get; set; }
public int Property5 { get; set; }
}
最终的结果会是这样
The end result would be something like this
A.Prorperty1
B.Property2
B.Property3
C.Property4
C.Property5
如果你注意到我不想接受,因为我搜索的属性的方式重写的属性,如果我做这样的事情
If you notice I don't want to accept overridden properties because of the way I search for the properties if I do something like this
C.Property3例如,它不能找到它,它会检查C'S基本类型有它会找到它。
C.Property3 for example and it cannot find it it will check C's basetype and there it will find it.
这是我到目前为止所。
public static void RegisterType( Type type )
{
PropertyInfo[] properties = type.GetProperties( BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.GetProperty | BindingFlags.SetProperty );
if ( properties != null && properties.Length > 0 )
{
foreach ( PropertyInfo property in properties )
{
// if the property is an indexers then we ignore them
if ( property.Name == "Item" && property.GetIndexParameters().Length > 0 )
continue;
// We don't want Arrays or Generic Property Types
if ( (property.PropertyType.IsArray || property.PropertyType.IsGenericType) )
continue;
// Register Property
}
}
}
我想主要有以下内容:
What I want are the following:
- 在公共属性,是的不可以覆盖,不可以静态的不可以私人
- 无论是获取和设置属性允许
- 在他们的不可以数组或一个泛型类型
- 他们在例子中,树即C级的顶端是最高的(属性列表的例子正是我期待的)
- 在他们的不可以一个索引属性(这[指数])
- Public properties, that are not overridden, not static, not private
- Either get and set properties are allowed
- They are not an array or a generic type
- They are the top of the tree ie C class in the example is the highest (The property list example is exactly what I am looking for)
- They are not an indexer property ( this[index] )
任何帮助将大大AP preciated =)。
Any help will be much appreciated =).
推荐答案
为了忽略继承的成员,您可以使用BindingFlags.DeclaredOnly标志,你已经做的事情。
In order to ignore inherited members, you can use the BindingFlags.DeclaredOnly flag, which you're already doing.
但是,当属性覆盖,它们是由派生类中重新声明。诀窍是再看看自己的访问方法,以确定他们是否实际上覆盖。
But when properties are overridden, they are re-declared by the derived class. The trick is to then look at their accessor methods to determine if they are in fact overridden.
Type type = typeof(Foo);
foreach ( var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
var getMethod = property.GetGetMethod(false);
if (getMethod.GetBaseDefinition() == getMethod) {
Console.WriteLine(getMethod);
}
}
如果该属性被覆盖,其吸的MethodInfo将从 GetBaseDefinition
返回不同的MethodInfo。
If the property is overridden, its 'getter' MethodInfo will return a different MethodInfo from GetBaseDefinition
.
这篇关于我如何确定,如果财产被重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!