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)

感觉我应该能够使用带有 Target 属性的 SupressMessage 来实现这一点。但是,因为这是一个框架方法,我不确定。

//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属性。如果将类更改为非泛型类(带有object Value),则警告消失。(一个泛型类object Value也给出了警告)。

当然,这并不能回答最初的问题,但我认为不可能这样做。

于 2012-01-12T23:59:45.553 回答
0
  1. 将 GlobalSuppressions.cs 添加到项目的根目录。

  2. 添加您的 [模块...

  3. 用汇编替换单词模块。

那样有用吗?

于 2012-01-07T02:09:29.610 回答
0

它确实有效。您可以将 a 添加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 回答