Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my own method similar to String.Format using Composite Formatting in C#

Tags:

I like how String.Format uses arguments to inject variables in to the string it is formatting. This is called Composite Formating and is discussed by MSDN here.

I want this functionality with my logging facade:

string foo = "fancy"; string bar = "message"; log.Debug("My {0} log {1}.", foo, bar) 

My ILoggerFacade has the following method signature:

void Debug<T>(T message, params Object[] args); 

And, I know I can implement this quite simply:

ILog m_Log = \\some logging implementation public void Debug<T>(T message, params Object[] args) {     m_Log.Debug(String.Format(message, args)); } 

However, in Visual Studio I don't get the fancy highlighting of the {0}, {1}, ... arguments:

Argument highlighting for Composite Formatting methods

I guess it is ReSharper who is resposible for them, and it seems like it is just ignoring the formatting arguments and giving no "intellisense" help. This isn't good since the other developers who will be using the facade will be expecting this.

How do I get argument highlighting and "intellisense" for custom formatted methods similar to how these work:

Console.WriteLine(...) String.Format(...) etc... 

Any help would be appreciated.

like image 826
André C. Andersen Avatar asked Aug 23 '12 13:08

André C. Andersen


People also ask

What is string format () and how can we use it?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.

What is %p format string?

%p expects the argument to be of type (void *) and prints out the address. Whereas %x converts an unsigned int to unsigned hexadecimal and prints out the result.


1 Answers

Check out ReSharpers External Annotations. Specifically, you want to use StringFormatMethodAttribute for this.

To use the External Annotations there are actually 3 methods. Two that it spells out, and one that you have to read between the lines to see.

  1. Reference "JetBrains.Annotations.dll". I would recommend against this one. I don't like the idea of copying the DLL, or having to reference the ReSharper install directory. This could cause issues if you upgrade or re-install.

  2. Copying and pasting attribute declarations into your solution. I'd recommend this as it gives you more control. Additionally, you can get rid of ReSharper (why would anyone do this? Stranger things have happened, I guess.), and still provide this feature to anyone that consumes your library. There are step by step instructions on how to do this in the first link.

  3. Create an XML file, similar to what it uses for for the .NET Assemblies. I did this for the Silverlight Unit Test Framework. ReSharper does not recognize these tests by default.

    To do this

    1. Create a file name <assembly>.xml and put it in "ReSharper\vXX\Bin\ExternalAnnotations".
    2. Add a root element "<assembly name="<assembly>">
    3. Now add <member> elements for each member that you want to give an attribute.

    I do not recommend doing this for your own code. However, if you have an assembly that you want to have this functionality, but cannot edit, this is the way to do it. This will only apply on your machine and each developer that uses the assembly will need to copy the xml file.

like image 164
cadrell0 Avatar answered Sep 28 '22 03:09

cadrell0