c# - Custom Attribute to Process info before a method is called -


i have create attribute process info before method called not getting called.

i want log values process , stored in static field in class, result of other methods of class called.

so can guide on it.

[attributeusage(attributetargets.method)] internal class myattrib : attribute {     public myattrib()     {         //this not getting called. missing         console.writeline("my attrib called!!");     } }  class myclass {     public myclass()     {         console.writeline("constructor created");     }      [myattrib]     public int opt1()     {         console.writeline("op1 performed");         return 0;     } 

}

static void main(string[] args) {         myclass cla = new myclass();         cla.opt1();         cla.opt2();         console.readline(); } 

attributes not instantiated during run-time. use reflection obtain attributes applied various parts of code (types, fields, etc) , contents of attributes are.

read this page on msdn regarding accessing attributes. specifically, part states:


an attribute specification such as:

[author("p. ackerman", version = 1.1)] class sampleclass 

is conceptually equivalent this:

author anonymousauthorobject = new author("p. ackerman"); anonymousauthorobject.version = 1.1; 

however, code not executed until sampleclass queried attributes. calling getcustomattributes on sampleclass causes author object constructed , initialized above.


one thing might able have base class other classes create derive from. in base class's constructor, use reflection identify attributes or else class you're interested in , information.

this doesn't address statement processing info before method executed, though... don't believe possible.


Comments