开发者

Dynamic string formatting in c#

开发者 https://www.devze.com 2023-03-12 11:57 出处:网络
I created log method that accept string. When I wan开发者_如何学运维t to use it I write something like:

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

精彩评论

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