inheritance - Reflection doesn't recognize that class is inherited C# -
my problem want data custom attributes, don't know argutement give typeof() method take current type.
[attrn("test1")] [attrc("test2")] public class cl2 :cl1 { //should find attributes , set fields base public cl2() : base() { } } public class cl1 { public string n; public string c; public cl1() { setvaluesfromcustomattributes(); } private void setvaluesfromcustomattributes() { foreach (var in attribute.getcustomattributes(typeof(cl1))) { setn(a); setc(a); } } private void setn(attribute attr) { if (attr attrnattribute) { var x = (attrnattribute)attr; n= x.getstring; } } private void setc(attribute attr) { if (attr attrcattribute) { var x = (attrcattribute)attr; c = x.getstring; } } }
just change line:
foreach (var in attribute.getcustomattributes(typeof(cl1)))
to
foreach (var in attribute.getcustomattributes(this.gettype()))
Comments
Post a Comment