How do I determine if a property was overriden?

Posted by Benjamin on Stack Overflow See other posts from Stack Overflow or by Benjamin
Published on 2010-12-22T00:33:24Z Indexed on 2010/12/22 0:54 UTC
Read the original article Hit count: 175

Filed under:
|
|
|
|

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.

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.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 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] )

Any help will be much appreciated =).

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET