Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 "ReSharpervXXBinExternalAnnotations".
    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...