Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Code Contracts ignore a specific assembly reference?

I'm making an extension to Visual Studio. Within the code I'm using Code Contracts to make assertions and checks. I set the warning option level to high.

What I would like to do is maintain that warning level while ignoring any checks made on EnvDTE references.

Consider the following code example:

public static string GetAbsoluteOutputFolder(EnvDTE.Project project) {     if (project == null) throw new ArgumentNullException("project");      var path =         project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString();     //... } 

With my current settings, CC would require me to add the following checks before assigning the path variable:

Contract.Assume(project.ConfigurationManager != null); Contract.Assume(project.ConfigurationManager.ActiveConfiguration != null); Contract.Assume(project.ConfigurationManager.ActiveConfiguration.Properties != null); 

Therefore what I'd like to do here is to tell CC to "trust" EnvDTE and ignore these types and their properties.

I thought the "Be optimistic on external API" CC option served this very purpose; turns out it doesn't.

Is there a way to make it behave the way I want that would not require a lower warning level?

EDIT: I want a solution that would work at project level and that would still allow "regular" checks to be performed.

like image 963
Crono Avatar asked Aug 29 '14 12:08

Crono


People also ask

Which of the following code elements are pure in code contracts in .NET framework?

Code contract tools currently assume that the following code elements are pure: Methods that are marked with the PureAttribute. Types that are marked with the PureAttribute (the attribute applies to all the type's methods). Property get accessors.

What is assembly reference in C#?

Reference assemblies are usually distributed with the Software Development Kit (SDK) of a particular platform or library. Using a reference assembly enables developers to build programs that target a specific library version without having the full implementation assembly for that version.

What do code contracts do?

Code Contracts provide a language-agnostic way to express coding assumptions in . NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.


1 Answers

Can´t provide a detailed solution but this should be solvable by using either the Baseline Feature or System.Diagnostics.CodeAnalysis.SuppressMessage on assembly level:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Contracts", "Whatever")] 

You can use the "Target" Property of the SuppressMessageAttribute to only ignore the Message on specific Types / Methods / Namespaces:

[SuppressMessage("Microsoft.Contracts",                   "CC1055",                   Scope="Member",                   Target="YouNamespace.EnvDTE.Project")] 

Note that the Parameters i used are just a good bet, you´ll have to figure out the correct Scope, MessageId and Target yourself :) On a sidenote, i think the Attribute is Conditional("CODE_ANALYSIS").

The official suggested solution to this problem is to create some sort of wrapper, in your case probably a repository that would create or contain your EnvDTE.Project objects. Then you can add the required Contract.Ensures there.

like image 72
Mecaveli Avatar answered Sep 24 '22 00:09

Mecaveli