mercredi 3 juin 2015

Reflection doesn't recognize that class is inherited C#

My Problem is I want to get the data from custom Attributes, I don't know which argutement to give the typeof() Method to take the current type.

[AttrN("test1")]
[AttrC("test2")]
public class cl2 :cl1 {
    //should find attributes and set fields from base 
    public cl2() : base() {   }
}

public class cl1 {
    public string n;
    public string c;

    public cl1() {
        setValuesFromCustomAttributes();
    }
    private void setValuesFromCustomAttributes() {
        foreach (var a in Attribute.GetCustomAttributes(typeof(cl1))) {
            setn(a);
            setc(a);
        }
    }
    private void setn(Attribute attr) {
        if (attr is AttrNAttribute) {
            var x = (AttrNAttribute)attr;
            n= x.getString;
        }
    }
    private void setc(Attribute attr) {
        if (attr is AttrCAttribute) {
            var x = (AttrCAttribute)attr;
            c = x.getString;
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire