【问题标题】:Can I use SuppressMessage on a framework method?我可以在框架方法上使用 SuppressMessage 吗?
【发布时间】:2011-12-22 20:02:05
【问题描述】:

我想实施来自 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 来实现这一点。不过,因为这是一个Framework方法,我不确定。

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

【问题讨论】:

  • 您是否有理由不使用Contract.Assume?出现次数太多了?
  • 我们试图远离 Contract.Assume,但是是的,有不少出现,我们会继续添加更多。
  • 我猜问题是获取表达式/方法信息的各种方法不Ensure 结果是非空的。您是否考虑过使用一些包装方法,例如:social.msdn.microsoft.com/Forums/en-NZ/codecontracts/thread/… 中给出的那些?这样您只需将Assume 放在一个地方,这样您的Assume 使用率就会降到最低。
  • 这是一个好主意,但问题是合约违反发生在对方法的调用中,而不是 within 方法(因为该方法需要传递一个表达式) .我将添加一个示例。
  • 在您的真实代码中,您是否通过表达式树构建n=&gt;n.Value?您的错误提到了Expression.Property,但示例中没有出现。

标签: c# code-contracts suppressmessage


【解决方案1】:

在对 ranomore 进行更多调查后,代码合同中似乎存在错误。

通过n =&gt; n.Value 访问的类有一个通用的T Value 属性。如果将类更改为非泛型类(使用object Value),警告就会消失。 (带有object Value 的泛型类也会发出警告)。

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

【讨论】:

  • 一定不是,否则早就有人回答了。 ;)
【解决方案2】:
  1. 将 GlobalSuppressions.cs 添加到项目的根目录。

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

  3. 将模块一词替换为程序集。

这行得通吗?

【讨论】:

  • 我寄予厚望,但可惜,它不起作用。我也删除了 Scope = "Member",以防万一。
【解决方案3】:

它确实有效。您可以将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);
}

明显的缺点是你将不得不用这些来涂抹你的代码......

【讨论】:

  • 那不是很危险吗?这就是 all 要求你在那里压制。我希望只压制一条消息。
  • @ranomore:是的,你也可以只屏蔽这一条消息。而不是Requires,您必须使用Requires-11-10,其中数字指定警告的确切位置。当您将以下内容添加到代码合同项目属性页面上的静态检查器的附加命令中时,您会从代码合同中获得这些数字:-outputwarnmask。对于每个警告,它将输出(在“输出”窗口中,而不是在错误列表中)一条消息,该消息为您提供完整的 SupressMessage 属性,您可以使用该属性来禁止该特定警告。
  • 我没有将它用于我的 NHibernate 映射类,因为我太懒了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
相关资源
最近更新 更多