i have c# class used in custom db orm tools, called dbfieldattribute.
i place on field, so:
[dbfield("user_id")] public int userid{ get; set; } challenge: attributes constructor code, fieldinfo of field associated attribute. in case above, give me fieldinfo userid.
any great. thanks.
unfortunately, best of knowledge, there no way accomplish asking for.
but if not necessary propertyinfo or fieldinfo object inside constructor, instead satisfied being passed method, there possible solution.
first of all, dbfield class need defined in following way.
class dbfield : attribute { public dbfield(string source) { } public void getinstance(propertyinfo source) { console.writeline(source.name); } } you need define following class (in case) properties marked dbfield attribute, , pass them getinstance(propertyinfo) method.
class activateattributes { public activateattributes(object source) { source.gettype() .getproperties() .where(x => x.getcustomattributes().oftype<dbfield>().any()) .tolist() .foreach(x => (x.getcustomattributes().oftype<dbfield>().first() dbfield).getinstance(x)); } } the way trigger process inside abstract class, defined so.
abstract class abstractdecoratedclass { public abstractdecoratedclass() { new activateattributes(this); } } now target class, has properties decorated dbfield attributes, needs derive class, won't bothered invocation inside constructor.
class decoratedclass : abstractdecoratedclass { [dbfield("user_id")] public int userid { get; set; } [dbfield("user_id2")] public int userid2 { get; set; } } you left testing solution shown here.
class program { static void main() { new decoratedclass(); console.read(); } }
Comments
Post a Comment