如何让我的自己的方法在C#中使用复合格式化类似的String.Format [英] How do I make my own method similar to String.Format using Composite Formatting in C#

查看:98
本文介绍了如何让我的自己的方法在C#中使用复合格式化类似的String.Format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢的String.Format如何使用论据,它正在格式化字符串注入变量。这就是所谓的复合格式化,并通过MSDN 这里讨论。

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)

我ILoggerFacade有以下方法签名:

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));
}

然而,在Visual Studio中我没有得到的花哨高亮{ 0},{1},...参数:

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

我猜是ReSharper的是谁resposible他们,现在看来似乎只是忽略了格式化参数,并没有给出智能感知的帮助。这不是因为谁将会使用外立面将被期待这个其他开发商好。

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.

推荐答案

查看ReSharpers 外部注释。具体来说,要使用 StringFormatMethodAttribute 这一点。

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

要使用外部注释居然有3种方法。二,它阐述了,一个你有字里行间看到的。

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. 参考JetBrains.Annotations。 DLL。我不推荐这一个。我不喜欢复制DLL,或者必须引用ReSharper的安装目录的想法。这可能导致问题,如果您升级或重新安装。

  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.

复制和粘贴属性声明到您的解决方案。我建议这个,因为它给你更多的控制权。此外,你可以摆脱ReSharper的(为什么会有人这样做?奇怪的事情已经发生了,我猜。),并仍然提供此功能的人,消耗你的图书馆。还有一步就如何做到这一点的第一个环节的指导。

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.

创建一个XML文件,类似于它使用了.NET程序集。我这样做是对Silverlight的单元测试框架。 ReSharper的默认不承认这些测试。

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.

要做到这一点


  1. 创建一个文件名 <装配方式>的.xml ,并把它放在ReSharper\vXX\Bin\ExternalAnnotations

  2. 添加根元素<装配NAME =<装配>>

  3. 现在,添加<成员> 为你想给一个属性的每个成员元素。

  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.

我不建议这样做你自己的代码。但是,如果您有想拥有此功能的组件,但不能编辑,这是做它的方式。这只会您的计算机,并使用该组件将需要复制的XML文件中的每个开发者的应用。

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.

这篇关于如何让我的自己的方法在C#中使用复合格式化类似的String.Format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆