5

CodeContracts からの次の提案を実装したいと思います。

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)

これを実現するには、SupressMessage を Target 属性と共に使用できるようにすべきだと思います。ただし、これはフレームワークのメソッドであるため、よくわかりません。

//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.")]

この警告をグローバルに抑制するにはどうすればよいですか? コールサイトのすべての警告をベースライン化または抑制する必要はありません。

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));
}
4

3 に答える 3

3

ranomoreでさらに調査したところ、コードコントラクトにバグがあるようです。

経由でアクセスされるクラスにn => n.Valueは、一般的なT Valueプロパティがあります。クラスが非ジェネリッククラス(with object Value)に変更されると、警告は消えます。(ジェネリッククラスobject Valueも警告を出します)。

もちろん、これは元の質問に答えるものではありませんが、それは不可能だと思います。

于 2012-01-12T23:59:45.553 に答える
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);
}

明らかな欠点は、これらでコードを塗りつぶさなければならないことです...

于 2013-02-21T17:29:30.730 に答える
0
  1. GlobalSuppressions.cs をプロジェクトのルートに追加します。

  2. [モジュールを追加...

  3. モジュールという単語をアセンブリに置き換えます。

それは動作しますか?

于 2012-01-07T02:09:29.610 に答える