jeudi 12 mai 2016

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

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?





Aucun commentaire:

Enregistrer un commentaire