1

Is there a way to "mark" properties of an object so they will "stand out" in reflection?

For example:

class A
{
    int aa, b;
    string s1, s2;

    public int AA
    {
        get { return aa; }
        set { aa = value; }
    }

    public string S1
    {
        get { return s1; }
        set { s1 = value; }
    }

    public string S2
    {
        get { return s2; }
        set { s2 = value; }
    }
}

class B : A
{
    double cc, d;
    C someOtherDataMember;

    public C SomeOtherDataMember
    {
        get { return someOtherDataMember; }
    }

    public double CC
    {
        get { return cc; }
    }

    public double D
    {
        get { return d; }
        set { d = value; }
    }
}

class C
{...}

I want to be able to act on the data members of B only, i.e to mark them so I'll be able to tell them apart from the members of A.

Like this:

    B b = new B();
    var x = b.GetType().GetProperties();
    foreach (PropertyInfo i in x)
    {
        if (/*property is marked*/)
        {
            Console.WriteLine(i.Name);
        }
    }

And it would be even better if it would be able to work without an instance of the object, e.g:

    var x = B.GetType().GetProperties();
    foreach (PropertyInfo i in x)
    {
        if (/*property is marked*/)
        {
            Console.WriteLine(i.Name);
        }
    }

Is it possible to do that?

shinzou
  • 5,850
  • 10
  • 60
  • 124
  • 3
    Look into custom attributes. Create one (or more) that mean (to you) whatever marking you desire, then annotate those members you want to mark. Look for the attribute on those members you're looking for. See the explanation and example [here at MSDN](https://msdn.microsoft.com/en-us/library/a4a92379(v=vs.110).aspx). – davidbak May 12 '16 at 22:02
  • See an example of how to use attributes on properties at http://stackoverflow.com/questions/3289198/custom-attribute-on-property-getting-type-and-value-of-attributed-property – NineBerry May 12 '16 at 22:03
  • 1
    If you want to list the properties that are declared by `B` and not inherited, you can use straight reflection; no need for custom attributes. – D Stanley May 12 '16 at 22:07

2 Answers2

5

I want to be able to act on the data members of B only, i.e to mark them so I'll be able to tell them apart from the members of A.

You can use custom attributes to add metadata to members, but it's not needed for what you want. You can use straight reflection. Look at DeclaringType, and use typeof(B) if you don't want to create an instance:

var x = typeof(B).GetProperties();
foreach (PropertyInfo i in x)
{
    if (i.DeclaringType == typeof(B))
    {
        Console.WriteLine(i.Name);
    }
}

you can also apply that filter when getting the properties:

var x = typeof(B).GetProperties(BindingFlags.DeclaredOnly
                              | BindingFlags.Public
                              | BindingFlags.Instance);
foreach (PropertyInfo i in x)
{
    Console.WriteLine(i.Name);
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
3

You can write your custom attributes, and then using GetCustomAttributes method you can check the properties.

GetCustomAttributes

For example

foreach (PropertyInfo property in properties)
{
    var ca = property.GetCustomAttributes(false);

    foreach (var attribute in ca)
    {
        if (attribute is YourAttribute)
        {
                        //...
        }

     }
}
Gusman
  • 14,905
  • 2
  • 34
  • 50
Eins
  • 155
  • 8
  • adding an example would higly increase the quality of this answer. – Gusman May 12 '16 at 22:07
  • Custom attributes are great for adding metadata to the objects, but a simple binding specification in the `GetProperties` might be simpler in this case. – Corey May 12 '16 at 22:23