2011-12-22 4 views
5

나는 CodeContracts에서 다음과 같은 제안을 구현하고 싶습니다 : 그것은 내가이 일어날 수 있도록 대상 속성 SupressMessage을 사용할 수있을 것 같은 느낌프레임 워크 메서드에서 SuppressMessage를 사용할 수 있습니까?

CodeContracts: MyModule: Method MyModule.MyClass.MyMethod: 
To mask *all* warnings issued like the precondition add the attribute: 

[SuppressMessage("Microsoft.Contracts", "RequiresAtCall-propertyAccessor != null")] 

to the method 

System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression,System.Reflection.MethodInfo) 

. 그러나 이것이 프레임 워크 방법이기 때문에 확실하지 않습니다.

//doesn't work 
[module: SuppressMessage("Microsoft.Contracts", "RequiresAtCall-propertyAccessor != null", Scope = "Member", Target = "System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression,System.Reflection.MethodInfo)", Justification = "This isn't covered by Linq Contracts yet.")] 

어떻게 전 세계적으로이 경고를 억제 할 수있는, 그래서 나는베이스 라인 또는 callsite 경고의 모두를 억제 할 필요가 없습니다?

EDIT: The specific usage that requires this measure is: 

void mymethod() 
{ 
    var myObserver = new PropertyObserver<MyViewModel>(); 
    //this line throws the error, within the n => n.Value expression 
    myObserver.RegisterHandler(n => n.Value, OnValueChanged); 
} 

public class PropertyObserver<TPropertySource> where TPropertySource : INotifyPropertyChanged 
{ 
    public PropertyObserver<TPropertySource> RegisterHandler(
     Expression<Func<TPropertySource, object>> expression, 
     Action<TPropertySource> handler) 
    { 
     //what this does is irrelevant; the violation occurs in the method call 
    } 
} 

//n => n.Value decompiles to the following 
public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor) 
{ 
    //and this line is the message I want to suppress, but it's in the .NET framework. 
    ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor"); 
    ValidateMethodInfo(propertyAccessor); 
    return Property (expression, GetProperty(propertyAccessor)); 
} 
+0

당신이'Contract.Assume'를 사용하지 않는 이유가 거기에 석고로이 ...와 코드가 것입니다? 너무 많은 사건? – porges

+0

우리는 Contract.Assume에서 일반적으로 멀어 지려고 노력했습니다. 그렇습니다. 몇 가지 사례가 있습니다. 우리는 계속 더 많이 추가하고 있습니다. –

+0

그렇다면 문제는 Expression/MethodInfos를 획득하는 다양한 방법이 결과가 null이 아니란 것을 '보장'하지 않는다는 것입니다. http://social.msdn.microsoft.com/Forums/en-NZ/codecontracts/thread/d8e2c2ad-de37-42ef-a854-02052d821975와 같은 래퍼 메서드를 사용 해본 적이 있습니까? 그렇게하면 한 장소에서'Assume '만하면되므로'Assume' 사용이 최소화됩니다. – porges

답변

3

ranomore에 대한 추가 조사가 끝나면 코드 계약서에 버그가있는 것으로 보입니다.

n => n.Value을 통해 액세스되는 클래스에는 일반 T Value 속성이 있습니다. 클래스가 비 제네릭 클래스 (object Value)로 변경되면 경고가 사라집니다. (object Value의 일반 클래스도 경고를 표시합니다.)

물론이 질문은 원래 질문에 대한 답변이 아니지만 가능한 것은 아니라고 생각합니다.

+0

다른 사람이 이미 대답했거나 다른 사람이 이미 답변했을 것입니다. ;) –

-1

프로젝트 속성의 빌드 탭을 살펴보십시오. "Suppress Warnings"필드가 있습니다.

/nowarn (C# Compiler Options)

+1

이들은 _compiler_ 경고가 아니며 _code contract_ 경고입니다. 내가 말할 수있는 한 번호가 없습니다. 전화를 걸거나 전화 할 수 없습니다. 5275 –

+0

아, 충분히주의를 기울이지 않았습니다. 이것은 잠을 자지 않을 때 지불하는 가격입니다. – Amy

+0

특정 버전의 설명서를 가리 키지 않는 한 버전 번호가 포함 된 MSDN 링크를 게시하지 마십시오. 문제는 사람들이 해당 페이지의 링크를 클릭하기 시작하고, 예를 들어 .NET 2.0에서 발견 된 것입니다. –

0
  1. 프로젝트의 루트에 GlobalSuppressions.cs를 추가합니다.

  2. 은 ... 당신의 [모듈을 추가

  3. 어셈블리와 단어 모듈을 교체합니다.

작동합니까?

+0

나는 희망이 있었지만, 슬프게도, 그것은 효과가 없다. Scope = "Member"도 삭제했습니다. –

0

실제로 작동합니다. 식이 포함 된 메서드에 SupressMessageAttribute을 추가 할 수 있습니다. RequiresAtCall을 사용하지 마십시오. 대신, Requires를 사용

[SuppressMessage("Microsoft.Contracts", "Requires", 
       Justification = "Bug in static checker")] 
public void Override(AutoMapping<Bookings> mapping) 
{ 
    Contract.Assume(mapping != null); 

    mapping.Id(x => x.Id); 
} 

명백한 단점은 당신이

+0

그래도 꽤 위험하지 않아? 그게 당신이 거기서 억압하고 있다는 것을 요구합니다. 나는 단지 하나의 메시지를 억압하기를 희망했다. –

+0

@ranomore : 예,이 메시지 하나만 표시하지 않을 수도 있습니다. 'Requires' 대신에 숫자가 정확한 경고 위치를 지정하는 곳에'Requires-11-10'을 사용해야합니다. 코드 계약 프로젝트 등록 정보 페이지에서 정적 검사기에 대한 추가 명령 인'-outputwarnmask'에 다음을 추가하면 코드 계약에서이 번호를 얻을 수 있습니다. 모든 경고에 대해 출력 창 (오류 목록 아님)에서 특정 경고를 표시하지 않을 수있는 전체 SupressMessage 특성을 제공하는 메시지를 출력합니다. –

+0

내가 너무 게으른 때문에 내 NHibernate 매핑 클래스에 이것을 사용하지 않았다 :) –