引用已编译的 dll 时,Resharper 代码注释不起作用

Resharper code annotations don't work when referencing the compiled dll

假设我有以下 class

public class MyClass
{
    [NotifyPropertyChangedInvocator("propertyName")]
    public void RespondToProperty(string propertyName)
    {
        //Some code here...
    }
}

当解决方案中的另一个项目使用此 class 时,注释起作用。 Resharper 会自动完成调用者 class 属性(应该如此)。 但是当我引用已编译的 dll 时,resharper 不会自动完成 属性 名称。

我也试过定义以下外部注解,但没有成功:

<assembly name=”MyAssembly">
    <member name=”M:MyAssembly.MyClass.RespondToProperty(System.String,System.String)”>
        <attribute ctor=”M:JetBrains.Annotations.NotifyPropertyChangedInvocatorAttribute.#ctor” />
    </member>
</assembly>

如果不引用项目代码,只使用编译后的 dll,注释是否可以工作?

如果您使用最新版本的注释 - 例如从 ReSharper 9 设置复制的默认源实现,或引用 official nuget package - 那么它们不会编译到生成的 dll 中,通过默认。

属性是应用 [Conditional("JETBRAINS_ANNOTATIONS")] 定义的。这意味着除非定义 JETBRAINS_ANNOTATIONS 符号,否则不会编译属性,但 ReSharper 仍然可以使用属性进行分析。如果您定义 JETBRAINS_ANNOTATIONS,属性将编译到生成的 dll 中,当生成的 dll 用作参考时,ReSharper 现在可以使用这些属性。

需要注意一件事 - 如果您通过 nuget 包引用 dll 并定义 JETBRAINS_ANNOTATIONS,那么生成的 dll 现在将直接引用 JetBrains.Annotations.dll,并且您需要将 JetBrains.Annotations.dll 与生成的 dll 一起发送。如果您通过源代码包含注释,它们将被编译到生成的 dll 中,并且您没有任何额外的二进制引用。