反射不承认 class 是继承的 C#

Reflection doesn't recognize that class is inherited C#

我的问题是我想从自定义属性中获取数据,我不知道给 typeof() 方法哪个参数来获取当前类型。

[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;
        }
    }
}

只需更改此行:

foreach (var a in Attribute.GetCustomAttributes(typeof(cl1)))

foreach (var a in Attribute.GetCustomAttributes(this.GetType()))