混合可选参数并且当不能简单地超载PARAMS [英] Mixing optional parameters and params when can't simply overload
问题描述
类似于<一href="http://stackoverflow.com/questions/3948971/c-sharp-4-0-optional-parameters-and-params-do-not-work-together">this问题,我想混可选参数的params关键字,这当然引起混淆。不幸的是,创造过载的答案是不行的,因为我想利用呼叫者信息属性,像这样的:
Similar to this question, I want to mix optional parameters with the params keyword, which of course creates ambiguity. Unfortunately, the answer of creating overloads does not work, as I want to take advantage of caller info attributes, like this:
public void Info(string message, [CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0, params object[] args)
{
_log.Info(BuildMessage(message, memberName, lineNumber), args);
}
创建无可选参数过载会改变通话现场,$ P $正常工作pventing这些特殊的参数。
Creating an overload without the optional parameters would change the call-site, preventing these particular parameters from working properly.
我找到了一个解决方案,几乎适用(尽管它的丑陋):
I found a solution that almost works (though it's ugly):
public void Info(string message, object arg0, [CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
_log.Info(BuildMessage(message, memberName, lineNumber), arg0);
}
public void Info(string message, object arg0, object arg1, [CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
_log.Info(BuildMessage(message, memberName, lineNumber), arg0, arg1);
}
这里的问题是,如果你指定一个字符串的最后一个参数,过载解决方案假定你打算显式地指定成员名
在接受更少的参数的过载,这是不期望的行为。
The problem here is that if you specify a string for the last argument, the overload resolution assumes you're intending to explicitly specify memberName
in the overload that takes fewer arguments, which is not the desired behavior.
是否有某种方式来完成这个(可能使用了一些我没有学过什么?新的属性)或已经我们只是达到什么样的自动神奇的编译器支持,可以给我们的界限?
Is there some way to accomplish this (perhaps using some new attributes I haven't learned about?) or have we simply reached the limits of what the auto-magical compiler support can give us?
推荐答案
我的prefered方式: 只有两个顶置charachters - 丑陋的语言黑客虽然;
My prefered way: Only two charachters overhead - ugly language 'hack' though;
public delegate void WriteDelegate(string message, params object[] args);
public static WriteDelegate Info(
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
return new WriteDelegate ((message,args)=>
{
_log.Info(BuildMessage(message, memberName , lineNumber ), args);
});
}
使用情况(提供你自己的实施 BuildMessage
Info()("hello world {0} {1} {2}",1,2,3);
另类
的方式我collegue上来,使这项工作是这样的:
The way my collegue came up to make this work was like this:
public static class DebugHelper
public static Tuple<string,int> GetCallerInfo(
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
return Tuple.Create(memberName,lineNumber);
}
}
在InfoMethod:
The InfoMethod:
public void Info(Tuple<string,int> info, string message, params object[] args)
{
_log.Info(BuildMessage(message, info.Item1, info.Item2), args);
}
用法:
instance.Info(DebugHelper.GetCallerInfo(),"This is some test {0} {1} {2}",1,2,3);
这篇关于混合可选参数并且当不能简单地超载PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!