混合可选参数并且当不能简单地超载PARAMS

编程入门 行业动态 更新时间:2024-10-23 21:32:35
本文介绍了混合可选参数并且当不能简单地超载PARAMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

类似于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

本文发布于:2023-10-18 11:14:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   参数   简单   PARAMS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!