ReSharper Web Help

Code Analysis features are available in: ReSharper Full Edition and ReSharper C# Edition

ReSharper 4.0 introduces the Annotated Framework that relies on two fundamental items:

  • Custom attributes in the JetBrains.Annotations namespace.
  • A set of external XML files that are used to annotate .NET Framework Class Library and NUnit Framework symbols.

Custom attributes in the JetBrains.Annotations namespace were introduced to annotate symbols - both in source code and in library code where you don't have access to source - the way that makes code analysis more efficient. These attributes are:

  • LocalizationRequiredAttribute (for elements to define whether they should be localized).
  • StringFormatMethodAttribute (for methods with format strings as parameters).
  • InvokerParameterNameAttribute (for methods with string literal arguments that should match one of caller parameters).
  • AssertionMethodAttribute (for assertion methods).
  • AssertionConditionAttribute (for condition parameters of assertion methods).
  • TerminatesProgramAttribute (for methods that terminate control flow).
  • CanBeNullAttribute (for values that can be null).
  • NotNullAttribute (for values that can not be null).
  • CannotApplyEqualityOperatorAttribute (for types that can not be compared using == or != operators).
  • BaseTypeRequiredAttribute (to force implementing/inheriting specific types).
  • UsedImplicitlyAttribute (to disallow marking an entity as unused).
  • MeansImplicitUseAttribute (to extend semantics of any other attribute to mean that the corresponding entity should not be marked as unused).

Some of these attributes are directly associated with ReSharper code inspection items (displayed as warnings by default) - for example,

  • StringFormatMethodAttribute is related to Problems in format string.
  • CanBeNullAttribute and NotNullAttribute are associated with Possible 'null' assignment to entity marked with 'Value cannot be null' attribute.
  • CannotApplyEqualityOperatorAttribute is bound to Compare with '==' types marked by 'CannotApplyEqualityOperatorAttribute'.

Exactly these attributes were used to annotate a huge amount of symbols from the .NET Framework Class Library, as well as NUnit Framework. Annotations for members of the standard class library increase the efficiency of code analysis that ReSharper carries out. For example, StringFormatMethodAttribute is used to annotate String.Format-like methods the way that ReSharper checks the number of provided arguments against the structure of format string and warns the user if a necessary argument is missing (see Defining Custom String Formatting Methods for guidelines). These pre-built annotations are stored as XML files in ReSharper_installation_dir\Bin\ExternalAnnotations.

You can use attributes in the JetBrains.Annotations namespace to annotate your own code to make ReSharper analyze it with greater accuracy and insight - for example, AssertionMethodAttribute and AssertionConditionAttribute are perfect to annotate custom assertion methods, as follows:

[AssertionMethod]
public static void Assert([AssertionCondition(AssertionConditionType.IS_TRUE)] bool
    condition, [NotNull] string message)
    {}

Another example of how custom attributes can be used is marking a method that never returns a null value with NotNull attribute, to prevent ReSharper from showing the Possible null-reference exception warning:

[NotNull]
public static TValue TryGetValue<TKey, TValue>
    ([NotNull] this IDictionary<TKey, TValue> dictionary, TKey key)
    {}

However, to make use of ReSharper code annotation attributes, you should reference them in one of two ways:

  • By adding a project reference to JetBrains.Annotations.dll in the ReSharper installation directory (or elsewhere if you chose to copy this assembly), or
  • By copying and pasting attribute declarations into your solution.

 Note

For guidelines on how to add a project reference, see MSDN: Adding and Removing Project References .
To copy and paste declarations of ReSharper code annotation attributes into your solution
  1. Choose ReSharper | Options | Code Inspection | Code Annotations in the menu bar.
  2. Click Copy default implementation to clipboard to copy declarations of ReSharper code annotation attributes.
  3. Click OK to close the ReSharper Options dialog box.
  4. Paste the default implementation into an empty C# code file in your project.
  5. If required, move the default implementation to another namespace.

If you choose to transfer the default implementation into an alternative namespace, you should mark that namespace as a container for declarations of the attributes by following the procedure below.

To mark a namespace as containing declarations of ReSharper code annotation attributes and set a default annotation namespace
  1. Choose ReSharper | Options | Code Inspection | Code Annotations in the menu bar.
  2. The namespace where you've transferred the code annotation attributes should be now listed in the Namespaces with code annotation attributes area. Check its entry to mark the namespace as containing the declarations of the attributes.
  3. Optionally, to configure this namespace as the default code annotation namespace, select it in the Default annotation namespace drop-down list in the lower part of the tab.
  4. Click OK to close the ReSharper Options dialog box.

See Also

Procedures

Reference

External Links