2012-12-26 2 views
0

나는 System.IServiceProvider.GetService 방법을 구현, 그리고 난 내가 GetService 같은데요 ... implements interface method 'System.IServiceProvider.GetService(System.Type)', thus cannot add Requires.CC1033 메시지 억제가 적용되지 않는 이유는 무엇입니까?

[SuppressMessage("Microsoft.Contracts","CC1033", Justification="Check to require service type not null cannot be applied")] 
public object GetService(Type serviceType) 
{ 
} 

답변

0

당신이 구체적인 클래스에서 구현 한 인터페이스의 방법으로 경고를 억제 할 수 없으며, 그 콘크리트의 방법 클래스에는 계약이 포함되어 있습니다. 이 계약은 인터페이스에 대한 계약을 제공하는 계약 클래스로 이동해야합니다. 전체 내용은 code contracts documentation (섹션 2.8)에서 찾을 수 있습니다. 다음은 추출물 :

을 유지하기 위해 별도의 계약 클래스를 만들 필요는 인터페이스 방법에 대한 계약을 작성, 인터페이스 내에서 메소드의 본체를 넣어 못하게 (C# 및 VB 포함) 대부분의 언어/컴파일러 이후 .

인터페이스와 계약 클래스는 한 쌍의 속성 (섹션 4.1)을 통해 연결됩니다.

[ContractClass(typeof(IFooContract))] 
interface IFoo 
{ 
    int Count { get; } 
    void Put(int value); 
} 

[ContractClassFor(typeof(IFoo))] 
abstract class IFooContract : IFoo 
{ 
    int IFoo.Count 
    { 
     get 
     { 
      Contract.Ensures(0 <= Contract.Result<int>()); 
      return default(int); // dummy return 
     } 
    } 
    void IFoo.Put(int value) 
    { 
     Contract.Requires(0 <= value); 
    } 
} 

이렇게하지 선호하는 경우, 그것은 어쨌든 적용되지 않는 같은 경고는 단순히 코드 계약을 제거 억제합니다.

UPDATE는

가 작동하는 것 같군하는 테스트 케이스이다.

namespace StackOverflow.ContractTest 
{ 
    using System; 
    using System.Diagnostics.Contracts; 

    public class Class1 : IServiceProvider 
    { 
     public object GetService(Type serviceType) 
     { 
      Contract.Requires<ArgumentNullException>(
       serviceType != null, 
       "serviceType"); 
      return new object(); 
     } 
    } 
} 

namespace StackOverflow.ContractTest 
{ 
    using System; 

    using NUnit.Framework; 

    [TestFixture] 
    public class Tests 
    { 
     [Test] 
     public void Class1_Contracts_AreEnforced() 
     { 
      var item = new Class1(); 
      Assert.Throws(
       Is.InstanceOf<ArgumentNullException>() 
        .And.Message.EqualTo("Precondition failed: serviceType != null serviceType\r\nParameter name: serviceType"), 
       () => item.GetService(null)); 
     } 
    } 
} 

시나리오와 다른 점이 있습니까?

+0

인터페이스가 Dot Net Framework에 의해 제공되므로 계약 클래스를 추가하는 방법을 사용할 수 있다고 생각하지 않습니다. –

+0

어떤 버전 코드 계약을 사용하고 있으며 재판이 있습니까? 나는 곧 나를 위해 일하는 테스트 케이스로 나의 대답을 업데이트 하겠지만, 나는 뭔가 빠뜨린 것처럼 보일 것이다. – Mightymuke

관련 문제