I have an object with a custom WriteLine(string)
method. Something like this:
我有一个对象,它有一个定制的WriteLine(string)方法。是这样的:
public void WriteLine(string text)
{
this.StringList.Add(text);
}
What is the easiest way to duplicate the functionality of string.Format()
with this method? For example: What I currently often find myself doing is this:
使用此方法复制string.Format()功能的最简单方法是什么?例如:我现在经常发现自己在做的是:
myObj.WriteLine(string.Format("Hello, {0}", name));
If you create a new Console application their version of a WriteLine()
method does exactly what I would prefer to do:
如果您创建了一个新的控制台应用程序,那么他们的WriteLine()方法的版本完全符合我的意愿:
Console.WriteLine("Hello, {0}", name);
They eliminate the need to call string.Format()
. Is it easy to make your method accept this somehow? Or am I going to have to create a thousand method overloads? Something like this:
它们消除了调用string.Format()的需要。让你的方法接受这一点容易吗?还是说我必须创建一千种方法重载?是这样的:
public void WriteLine() { ... }
public void WriteLine(string text) { ... }
public void WriteLine(string text, object arg0) { ... }
public void WriteLine(string text, object arg0, object arg1) { ... }
public void WriteLine(string text, object arg0, object arg1, object arg2)
{
this.StringList.Add(string.Format(text, arg0, arg1, arg2));
}
// etc etc etc
Is that the only logical way to do this? Any suggestions are welcomed :)
这是唯一合乎逻辑的方法吗?欢迎提出建议:)
3 个解决方案
#1
61
You need to copy the method signature from string.format.
您需要从string.format中复制方法签名。
public void WriteLine(string text,params object[] args) {
this.StringList.Add(string.Format(text,args));
}
As suggested by ChaosPandion you can also include overloads to prevent array creation
正如ChaosPandion所建议的,您还可以包含重载以防止数组创建
public void WriteLine(string text) {
this.StringList.Add(text);
}
public void WriteLine(string text,object arg0) {
this.StringList.Add(string.Format(text, arg0));
}
public void WriteLine(string text,object arg0, object arg1) {
this.StringList.Add(string.Format(text, arg0, arg1));
}
public void WriteLine(string text,object arg0, object arg1, object arg2) {
this.StringList.Add(string.Format(text, arg0, arg1, arg2));
}
I wouldn't go past arg2 as string.format doesn't so the benefit disappears.
我不会以字符串的形式通过arg2。格式并不会让好处消失。
#2
7
You could use params
:
您可以使用参数:
public void WriteLine(string text, params object[] parameters)
{
//..
}
#3
7
There is one edge case you might want to take pains to avoid. The following code will write the string {0} to the console
有一个你可能想要避免的边缘情况。下面的代码将把字符串{0}写到控制台
Console.WriteLine("{0}");
However, you use one of the implementations of WriteLine suggested by others in this answer, those implementations will throw an exception:
但是,您使用了本文中其他人建议的WriteLine的一个实现,这些实现将抛出一个异常:
WriteLine("{0}"); //throws exception
The easiest fix is either to have two overloads of WriteLine or to modify the suggested code slightly to handle the edge case:
最简单的解决方案是要么有两个重载的WriteLine,要么稍微修改建议的代码来处理边缘情况:
public void WriteLine(string text,params object[] args) {
var message=args.Length==0 ? text : string.Format(text, args);
this.StringList.Add(message);
}
#1
61
You need to copy the method signature from string.format.
您需要从string.format中复制方法签名。
public void WriteLine(string text,params object[] args) {
this.StringList.Add(string.Format(text,args));
}
As suggested by ChaosPandion you can also include overloads to prevent array creation
正如ChaosPandion所建议的,您还可以包含重载以防止数组创建
public void WriteLine(string text) {
this.StringList.Add(text);
}
public void WriteLine(string text,object arg0) {
this.StringList.Add(string.Format(text, arg0));
}
public void WriteLine(string text,object arg0, object arg1) {
this.StringList.Add(string.Format(text, arg0, arg1));
}
public void WriteLine(string text,object arg0, object arg1, object arg2) {
this.StringList.Add(string.Format(text, arg0, arg1, arg2));
}
I wouldn't go past arg2 as string.format doesn't so the benefit disappears.
我不会以字符串的形式通过arg2。格式并不会让好处消失。
#2
7
You could use params
:
您可以使用参数:
public void WriteLine(string text, params object[] parameters)
{
//..
}
#3
7
There is one edge case you might want to take pains to avoid. The following code will write the string {0} to the console
有一个你可能想要避免的边缘情况。下面的代码将把字符串{0}写到控制台
Console.WriteLine("{0}");
However, you use one of the implementations of WriteLine suggested by others in this answer, those implementations will throw an exception:
但是,您使用了本文中其他人建议的WriteLine的一个实现,这些实现将抛出一个异常:
WriteLine("{0}"); //throws exception
The easiest fix is either to have two overloads of WriteLine or to modify the suggested code slightly to handle the edge case:
最简单的解决方案是要么有两个重载的WriteLine,要么稍微修改建议的代码来处理边缘情况:
public void WriteLine(string text,params object[] args) {
var message=args.Length==0 ? text : string.Format(text, args);
this.StringList.Add(message);
}