是,as noted in my answer here,作爲框架的4.5.2版本(可能是4.5)可以通知代碼分析所執行的代碼合同。擴展方法和標記屬性類必須如下定義:
public static class ContractExtensions {
/// <summary>Throws <c>ContractException{name}</c> if <c>value</c> is null.</summary>
/// <param name="value">Value to be tested.</param>
/// <param name="name">Name of the parameter being tested, for use in the exception thrown.</param>
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value")]
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "name")]
[ContractAbbreviator] // Requires Assemble Mode = Standard Contract Requires
public static void ContractedNotNull<T>([ValidatedNotNull]this T value, string name) where T : class {
Contract.Requires(value != null,name);
}
}
/// <summary>Decorator for an incoming parameter that is contractually enforced as NotNull.</summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class ValidatedNotNullAttribute : global::System.Attribute {}
其他的細節在我的其他答案。
我希望你可以寫一個FxCop插件來完成這項工作。 – GaTechThomas 2010-06-08 16:01:51
Terje Sandstrom有一個很棒的博客文章可以回答這個問題:http://geekswithblogs.net/terje/archive/2010/10/14/making-static-code-analysis-and-code-contracts-work-together-or。 aspx – 2010-12-10 07:18:21
@AngericoCariño我已經更新了上面提到的一些關於這個問題解決方法的新信息。 – 2012-02-19 22:17:44