Using extension methods inside the extended class itself

Say I have an interface, for example:

public interface ILoggable
{
    void Log(Func<string> message, Logger.Type type);
}

And some extension methods, for example:

public static class Logger
{
    public static void Log(this ILoggable loggable, Func<string> message) { loggable.Log(message, Type.Information); }
    public static void Log(this ILoggable loggable, string prefix, byte[] data, int len) { /* snip */ }
    public static void Log(this ILoggable loggable, Exception ex) { /* snip */ }
    // And so on...
}

Then, in any class CoreService : ServiceBase, ILoggableor such, I implement this public void Log(Func<string> message, Logger.Type type)for what I like (the public modifier is kind of meh ...) and uses all the extension methods for actually logging.

Still so good ... or not so good? Is there something wrong with this approach? If not, then why the inconvenience:

catch (Exception ex) {
    this.Log(ex); // this works
    Log(ex); // this goes not
+5
source share
1 answer

, 1 this , . , , ( ), , , , .

( 7.6.2 # 4) , . , , . .

, , - 7.6.5.2 ( ), " " .


1 , , , , - , , , .

+3

All Articles