Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force my code to use my extension method

Tags:

c#

I'm using BitFactory logging, which exposes a bunch of methods like this:

public void LogWarning(object aCategory, object anObject)

I've got an extension method that makes this a bit nicer for our logging needs:

public static void LogWarning(this CompositeLogger logger, 
      string message = "", params object[] parameters)

Which just wraps up some common logging operations, and means I can log like:

Logging.LogWarning("Something bad happened to the {0}. Id was {1}",foo,bar);

But when I only have one string in my params object[], then my extension method won't be called, instead the original method will be chosen.

Apart from naming my method something else, is there a way I can stop this from happening?

like image 460
Matt Roberts Avatar asked Mar 06 '14 11:03

Matt Roberts


People also ask

Is it possible to override the extension method?

Extension methods cannot be overridden the way classes and instance methods are. They are overridden by a slight trick in how the compiler selects which extension method to use by using "closeness" of the method to the caller via namespaces.

How do you call an extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.

Should I use extension methods C#?

Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.

What parameter does an extend () method requires?

Parameters of extend() function in Python The list extend() method has only one parameter, and that is an iterable. The iterable can be a list, tuple (it is like an immutable list), string, set or, even a dictionary.


1 Answers

The rules about how overloaded methods are resolved to one (or an error) are complex (the C# specification is included with Visual Studio for all the gory details).

But there is one simple rule: extension methods are only considered if there is no possible member that can be called.

Because the signature of two objects will accept any two parameters, any call with two parameters will match that member. Thus no extension methods will considered as possibilities.

You could pass a third parameter (eg. String.Empty) and not use it in the format.

Or, and I suspect this is better, to avoid possible interactions with additions to the library (variable length argument list methods are prone to this) rename to LogWarningFormat (akin to the naming of StringBuffer.AppendFormat).

PS. there is no point having a default for the message parameter: it will never used unless you pass no arguments: but that would log nothing.

like image 150
Richard Avatar answered Oct 14 '22 04:10

Richard