Question

I need to analyse my object and show some properties of it in a list. My object has some properties, which come from base class "ObjectBaseClass", these properties should not be shown in my list.

How can i know, wheather a property is from base-class or not?

i.e I have a interface IDisposableExtended, this interface will be implemented in my object class. But in my list, i don't want to show these two properties "Disposable" and "Disposed". How can i filter them?

public interface IDisposableExtended : IDisposable
{
    bool Disposable { get; }
    bool Disposed { get; }
}

Thanks a lot!

p.s The properties can come from a base-class(level 1), base-class(level 1) can also have some properties from his own base-class(level 2). Is that so, when i use GetProperties(flags), which contains BindingFlags.DeclaredOnly, all the properties come from base-classes (level 1 and level 2) will be filtered? Can i just filter base-class level 1 or level 2? That means, i want to get all properties first, and filter then according to their base-class manually. Then i have all control of them to show the properties, which i need.

Was it helpful?

Solution

You should use the Type.GetProperty(String, BindingFlags) method overload and include BindingFlags.DeclaredOnly to restrict the search to members that are not inherited.

Read more here: http://msdn.microsoft.com/en-us/library/kz0a8sxy(v=vs.110).aspx

In response to the comment (even though I'm not sure if I understand it correctly). Here is a quick program which will traverse the type hierarchy and display the properties declared on the type itself, excluding inherited ones at each level.

namespace ConsoleApplication1
{
    class ClassA
    {
        public string ClassAProp { get; set; }
    }

    class ClassB : ClassA
    {
        public string ClassBProp { get; set; }
    }

    class ClassC : ClassB
    {
        public string ClassCProp { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var c = new ClassC();
            var t = c.GetType();
            while (t.BaseType != null)
            {
                var cProps = t.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
                foreach (var p in cProps)
                {
                    Console.WriteLine("{0} defines {1}", t.Name, p.Name);
                }
                t = t.BaseType;
            }
            Console.ReadLine();
        }
    }
}

So, you can see 'BindingFlags.DeclaredOnly' returns only the properties declared on the class. Hope this helps to achieve what you are looking for.

Note: Type.BaseType will be null for the object type, as it's the root type. If you want the loop to stop at any other type, you should be able to say while (t != TypeYouWantToStopAt.GetType())

OTHER TIPS

First of all, the "owner" for these properties are actually the class, not the interface. The interface gives access to those properties, but the declaring type of those properties is still the class.

However, properties you inherit will have a different declaring type when you discover them through reflection. Here's a LINQPad program that demonstrates:

void Main()
{
    var derivedType = typeof(Derived);
    derivedType.GetProperties().Dump("All");
    derivedType.GetProperties(BindingFlags.DeclaredOnly
        | BindingFlags.Public | BindingFlags.Instance).Dump("Declared only");
}

public class Base
{
    public string BaseProperty { get; set; }
}

public class Derived : Base
{
    public string DerivedProperty { get; set; }
}

Note that if you also implement an interface in Derived, those methods/properties will also be shown when dumping out the properties of Derived, since they are declared by that type. Yes, they are used to implement the interface, but they are also declared ("owned") by the type.

This is slightly off from your question, but may give you an idea for a different approach to the problem you describe.

You could declare a custom attribute that you use to mark any property (or any member really) that you don't want to list. When you enumerate the properties of your object you can inspect it's attributes and skip over anything you have marked. This is similar to the way the .NET property Editor control works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top