首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在框架方法上使用SuppressMessage吗?

我可以在框架方法上使用SuppressMessage吗?
EN

Stack Overflow用户
提问于 2011-12-22 20:02:05
回答 3查看 1.6K关注 0票数 5

我想执行CodeContracts的以下建议:

代码语言:javascript
复制
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来实现这一点。但是,因为这是一个Framework方法,所以我不确定。

代码语言:javascript
复制
//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.")]

如何在全局范围内抑制此警告,从而不必对所有的呼叫站点警告进行基线或取消?

代码语言:javascript
复制
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));
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-12 23:59:45

在对ranomore进行了更多的调查之后,代码契约中似乎出现了一个缺陷。

通过n => n.Value访问的类具有一个泛型T Value属性。如果类被更改为非泛型类(带有object Value),则警告将消失。(带有object Value的泛型类也会发出警告)。

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

票数 3
EN

Stack Overflow用户

发布于 2012-01-07 02:09:29

  1. 将GlobalSuppressions.cs添加到项目的根。
  2. 将word模块添加到程序集中。

那有用吗?

票数 0
EN

Stack Overflow用户

发布于 2013-02-21 17:29:30

实际上很管用。可以将SupressMessageAttribute添加到包含表达式的方法中。只是不要使用RequiresAtCall。相反,使用Requires

代码语言:javascript
复制
[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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8609110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档