예 as noted in my answer here을 (아마도 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
Terje Sandstrom은 이에 대한 답변을 제공하는 훌륭한 블로그 항목을 가지고 있습니다. http://geekswithblogs.net/terje/archive/2010/10/14/making-static-code-analysis-and-code-contracts-work-together-or. aspx –
@Angerico Cariño이 문제에 대한 해결 방법과 관련된 몇 가지 새로운 정보로 위에서 언급 한 블로그 게시물을 업데이트했습니다. –