c# - Force my code to use my extension method -
i'm using bitfactory logging, exposes bunch of methods this:
public void logwarning(object acategory, object anobject)
i've got extension method makes bit nicer our logging needs:
public static void logwarning(this compositelogger logger, string message = "", params object[] parameters)
which wraps common logging operations, , means can log like:
logging.logwarning("something bad happened {0}. id {1}",foo,bar);
but when have 1 string in params object[]
, extension method won't called, instead original method chosen.
apart naming method else, there way can stop happening?
the rules how overloaded methods resolved 1 (or error) complex (the c# specification included visual studio gory details).
but there 1 simple rule: extension methods considered if there no possible member can called.
because signature of 2 objects accept 2 parameters, call 2 parameters match member. no extension methods considered possibilities.
you pass third parameter (eg. string.empty
) , not use in format.
or, , suspect better, avoid possible interactions additions library (variable length argument list methods prone this) rename logwarningformat
(akin naming of stringbuffer.appendformat
).
ps. there no point having default message
parameter: never used unless pass no arguments: log nothing.
Comments
Post a Comment