简体   繁体   English

以非侵入方式将Resharper代码注释添加到自己的代码中

[英]Adding Resharper code annotation to own code in a non-invasive manner

I want to flag one of my methods with the StringFormatMethod attribute so Resharper syntax highlights it. 我想用StringFormatMethod属性标记我的一个方法,因此Resharper语法突出显示它。

I was able to do this by referencing the JerBrains.Annotations assembly and adding the attribute. 我能够通过引用JerBrains.Annotations程序集并添加属性来完成此操作。

However I find this to be a very invasive way to do this. 但是我发现这是一种非常有创意的方法。 Not everybody here uses JetBrains and it will require committing the .dll to subversion, adding the dependency and littering the code with something that is specific to a particular IDE, which I hate. 这里并不是每个人都使用JetBrains,它需要将.dll提交给subversion,添加依赖项,并使用特定于特定IDE的内容乱丢代码,我讨厌。

I read about the "external annotations" feature, but I wasn't able to do it. 我读到了“外部注释”功能,但我无法做到。 I'm not sure if I did it wrong or if it's simply not supported for a project inside the solution (ie not a compiled assembly reference). 我不确定我是否做错了,或者解决方案中的项目是否完全不受支持(即不是编译的程序集引用)。

So is there a way to add a code annotation to a method in the project in a non-invasive way? 那么有没有办法以非侵入方式向项目中的方法添加代码注释?

PS this is the method: PS这是方法:

using System; 使用系统;

namespace MyLib
{
    public static class Assert
    {
        public static void That(bool condition, string format, params object[] @params)
        {
            if (!condition)
                throw new Exception(string.Format(format, @params));
        }
    }
}

And this is the contents of 这是内容

C:\\Program Files (x86)\\JetBrains\\ReSharper\\v7.1\\Bin\\ExternalAnnotations\\MyLib.xml: C:\\ Program Files(x86)\\ JetBrains \\ ReSharper \\ v7.1 \\ Bin \\ ExternalAnnotations \\ MyLib.xml:

<assembley name="MyLib">
    <member name="MyLib.Assert.That(System.Boolean,System.String,System.Object[])">
        <attribute ctor="M:JetBrains.Annotations.StringFormatMethodAttribute.#ctor">
            <argument>format</argument>
        </attribute>
    </member>
</assembley>

Just to sum up possibilities: 只是总结一下可能性:

  • You reference nuget Jetbrains.Annotations , and DO NOT define JETBRAINS_ANNOTATIONS : Such annotations are useful only for developers working with source code, they are not compiled in your binary (Conditional statement is used) and they are not visible when referencing your dll. 您引用了nuget Jetbrains.Annotations ,并且没有定义JETBRAINS_ANNOTATIONS这样的注释仅对使用源代码的开发人员有用,它们不会在您的二进制文件中编译(使用条件语句),并且在引用您的dll时它们不可见。 You can even add developmentOnly="true" attribute to Jetbrains.Annotations in packages.config, so by default it would not be treated as dependency. 您甚至可以将packagesOnly developmentOnly="true"属性添加到packages.config中的Jetbrains.Annotations ,因此默认情况下它不会被视为依赖项。

  • You reference as above but define JETBRAINS_ANNOTATIONS : now you have real binary dependency and Jetbrains.Annotations.dll must be either distributed with your library or it must be downloaded as nuget dependency. 您如上所述引用但是定义了JETBRAINS_ANNOTATIONS现在您有真正的二进制依赖项,并且Jetbrains.Annotations.dll必须与您的库一起分发,或者它必须作为nuget依赖项下载。

  • You copy annotations with internal checked (so client code would not use them), into "YourLib.Annotations": They then embedded into your lib and available for other developers even when they use only binary version. 您将带有internal选中的注释(因此客户端代码不会使用它们)复制到“YourLib.Annotations”中:然后它们嵌入到您的lib中,即使只使用二进制版本也可供其他开发人员使用。

  • You provide external annotations: for bigger libraries/more attributes this can also consume 40k, it is separate file, and generally it is less trivial to create/consume. 您提供外部注释:对于更大的库/更多属性,这也可以消耗40k,它是单独的文件,并且通常创建/使用不那么简单。

I personally went with third option (for shared libraries, projects usually just use nugets) 我亲自去了第三个选项(对于共享库,项目通常只使用nugets)

You don't have to reference the assembly to add annotation attributes. 您不必引用程序集来添加注释属性。 As per the documentation , you can go to ReSharper/Options/Code Annotations , copy the attribute implementations to the clipboard, and paste them into your own source, where ReSharper will use them. 根据文档 ,您可以转到ReSharper/Options/Code Annotations ,将属性实现复制到剪贴板,然后将它们粘贴到您自己的源中,ReSharper将使用它们。 You can even change the namespace they're in if you'd prefer not to have JetBrains in your assembly. 如果您不想在程序集中安装JetBrains ,甚至可以更改它们所在的命名空间。

I don't know whether you'll have any luck using external (XML) annotations for source code, though. 不过,我不知道你是否有幸使用外部(XML)注释来获取源代码。 I get the impression they're only for existing binaries. 我得到的印象是他们只适用于现有的二进制文件。 That said, I think that decorating your source with attributes is quite valuable as a source of documentation for yourself and other developers. 也就是说,我认为用属性来装饰你的源代码作为自己和其他开发人员的文档来源是非常有价值的。

Don't know if it helps, but the element name <assembley> is misspelled (unless they actually used that in the schema). 不知道它是否有帮助,但元素名称<assembley>拼写错误(除非它们实际上在模式中使用了它)。 Should be <assembly> . 应该是<assembly>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM