如何确定某个属性是否被覆盖? [英] How do I determine if a property was overridden?
问题描述
我正在做一个项目,在该项目中我需要注册所有属性,因为系统是如此之大,因此需要大量工作来注册我想依赖于Xaml的所有属性。 / p>
目标是查找树顶部的所有属性。
基本上这样
公共类A {
public int Property1 {get;组; }
}
公共类B:A {
public int Property2 {get;组; }
public virtual int Property3 {get;组; }
}
公共类C:B {
公共重写int Property3 {get;组; }
public int Property4 {get;组; }
public int Property5 {get;组; }
}
最终结果将是这样
A.Property1
B.Property2
B.Property3
C.Property4
C.Property5
如果您注意到我不想接受覆盖的属性,因为我搜索属性的方式做这样的事情
C.Property3,它找不到它会检查C的基类型,然后就可以找到它。
这是我到目前为止所拥有的。
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属性)
{
//如果属性是索引器,则我们将忽略它们
if(property.Name == Item&& property.GetIndexParameters()。Length> 0)
Continue;
//我们不希望数组或通用属性类型
if(((property.PropertyType.IsArray || property.PropertyType.IsGenericType))
继续;
//注册财产
}
}
}
我想要的是以下内容:
- 未被覆盖的公共属性, 不静态,不私有
- 允许get和set属性
- 它们是不是数组或通用类型
- 它们是树的顶部,即示例中的C类是最高的(属性列表示例正是我
- 它们不是索引器属性(this [index])
为了忽略继承的成员,可以使用BindingFlags.DeclaredOnly标志,该标志已经在执行。
但是当属性被覆盖时,它们被派生类重新声明。诀窍是然后查看其访问器方法以确定它们是否实际上已被覆盖。
Type type = typeof(Foo) ;
foreach(类型为var的属性。GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)){
var getMethod = property.GetGetMethod(false);
if(getMethod.GetBaseDefinition()== getMethod){
Console.WriteLine(getMethod);
}
}
如果该属性被覆盖,则其 getter方法信息将从 GetBaseDefinition
返回不同的MethodInfo。
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.
so basically
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.Property1
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 for example and it cannot find it it will check C's basetype and there it will find it.
This is what I have so far.
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:
- 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] )
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);
}
}
If the property is overridden, its 'getter' MethodInfo will return a different MethodInfo from GetBaseDefinition
.
这篇关于如何确定某个属性是否被覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!