c# - Using attributes to start function -


i have function uses stopwatch instance , measure time of method put inside. now, have start , stop every method. want using attribute. want place attribute on every method automatically starts , stops function measures time.

the code of function:

 [datacontract] [serializable]  public class apptrace:basemodel<apptrace> {     [datamember]     public string comment;     [datamember]     public string methodname;     [datamember]     public datetime starttimestamp;     [datamember]     public int duration;     [datamember]     public int userobjectid;     [datamember]     public string machinename;     [datamember]     public int toolid;     private system.diagnostics.stopwatch stpwatch;  public apptrace(string comment,string methodname,int userobjectid ,string machinename = "",int? toolid=null)      {         methodname = methodname;         userobjectid = userobjectid;         starttimestamp = datetime.utcnow;         comment = comment;         machinename = machinename;          stpwatch = new system.diagnostics.stopwatch();         stpwatch.start();     }      public apptrace()     {     }      public void closetrace()     {         this.stpwatch.stop();         duration=convert.toint32( this.stpwatch.elapsedmilliseconds);     } 

that won't work .net framework. aspect oriented programming implemented various third party providers, postsharp being used.

you can implement own attributes, without aop framework, not execute code you. metadata can query reflection.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -