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

自闭症网瘾萝莉.ら 提交于 2019-12-04 22:18:03
Mecaveli

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.

I don't think it's possible to solve the problem but since C# 6.0 there is a workaround which at least eases the pain a bit:

Instead of

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

you can now write

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

Have you tried something with : [assembly: Contracts.ContractVerification(false)] at the assembly level ?

You should be able to do it dynamically : https://msdn.microsoft.com/en-us/library/bb458043.aspx

Hope this helps,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!