c# - Override a method in the third party -


i have third party party class log. contains methods

public void write(string s, params object[] args) {             monitor.enter(this);             try             {                             string logentry = string.format(s, args);                             this.write(logentry);             }                         {                             monitor.exit(this);             } } 

and

public void write(string logentry) {     this.write(0, logentry); } 

i defined property outlog in own class

public static log outlog  {get;set;} 

i want use feature "callermembernameattribute" etc. in .net 4.5 , override write method like:

public void write(string s,     [callermembername] string membername = "",     [callerfilepath] string sourcefilepath = "",     [callerlinenumber] int sourcelinenumber = 0)     {         monitor.enter(this);         try         {             string logentry = string.format(s, membername, sourcefilepath, sourcelinenumber);             this.write(logentry);         }                 {             monitor.exit(this);         }     } 

so can call in class:

outlog.write(...); 

not sure how?

you can't override it, can create extension method (which signified this keyword on first parameter) want. like:

public static class logextensions {     public static void writewithcallerinfo(         log log,         string s,         [callermembername] string membername = "",         [callerfilepath] string sourcefilepath = "",         [callerlinenumber] int sourcelinenumber = 0)     {         monitor.enter(log);         try         {             string logentry = string.format(s, membername, sourcefilepath, sourcelinenumber);             log.write(logentry);         }                 {             monitor.exit(log);         }     } } 

with this, can write:

outlog.writewithcallerinfo("whatever"); 

the extension method can't called write, because normal methods take precedence on extension methods.

note don't understand reason locking, think shouldn't necessary, assuming overload write(int, string) thread-safe.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -