开发者

Dynamic string formatting in c#

I created log method that accept string. When I wan开发者_如何学运维t to use it I write something like:

Log(string.Format("Message {0}", AdditionalInfo));

How should I implement Log method in order to be able to use string Format but do not have to write it explicitly in method arguments:

Log("Message {0}", AdditionalInfo);

I use .net 2.0


public void Log(string formatString, params object[] parameters)
{
    Log(String.Format(formatString, parameters));
}


public void Log(string format, params object[] args)
{
    DoTheLog(string.Format(format, args));
}


void Log(string format, params object[] args)
{
  Log(string.Format(format, args));
}


Try something like:

public void Log(string format, params object[] arguments)
{
    string message = string.Format(format, arguments);
    // Do something with message.
}


You could perhaps write a wrapper for your Log method. I hope something like this works.

public void Log(string format, object args)
{
    Do_Log(string.Format(format, args))
}

Now you can have Do_Log() as private and not expose it to consumers of your class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜