0

I've created the following extension method to log4net's ILog interface:

public static void Debug(this ILog log, IDiagnosticMessage message)
{
    // do some stuff with the IDiagnosticMessage and end up calling ILog.Logger.Log with the result
}

The compiler doesn't seem to want to use this method. When I call ILog.Debug with an IDiagnosticMessage parameter it always uses log4net's ILog.Debug(object) overload.

Short of explicitly calling the static method does anybody have any ideas what I can do to make the compiler call my extension instead of the interface method?

Dean Ward
  • 4,793
  • 1
  • 29
  • 36
  • 3
    Give it a different name? – Oded Jun 05 '13 at 12:28
  • http://stackoverflow.com/a/13524408/1583 – Oded Jun 05 '13 at 12:29
  • 2
    An original method with a matching signature will always be executed in favour of an extension method - why can't you use a different method name? – Ant P Jun 05 '13 at 12:30
  • http://stackoverflow.com/questions/899539/is-there-any-way-in-c-sharp-to-override-a-class-method-with-an-extension-method –  Jun 05 '13 at 12:32
  • No reason at all, just looks nicer :)! I assumed that extension methods could override instance methods! Should have read the language spec before posting... 7.6.5.2 Extension method invocations in http://msdn.microsoft.com/en-us/library/vstudio/ms228593.aspx if anybody's interested! – Dean Ward Jun 05 '13 at 12:33
  • I've voted to close the question as duplicate, thanks! – Dean Ward Jun 05 '13 at 12:34

0 Answers0