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

发布于 2024-12-22 11:05:25 字数 1973 浏览 2 评论 0原文

我想实现 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));
}

I would like to implement the following suggestion from 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)

It feels like I should be able to use SupressMessage with the Target attribute to make this happen. However, because this is a Framework method, I'm not sure.

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

How can I globally suppress this warning, so I don't have to baseline or suppress all of the callsite warnings?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠栀 2024-12-29 11:05:25

经过对 ranomore 的进一步调查,代码合约中似乎存在一个错误。

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

当然,这并没有回答最初的问题,但我认为不可能做到这一点。

After some more investigation with ranomore, there seems to be a bug in Code Contracts.

The class being accessed via n => n.Value has a generic T Value property. If the class is changed to a non-generic class (with object Value) the warning disappears. (A generic class with object Value also gives the warning).

Of course, this doesn't answer the original question, but I don't think it's possible to do that.

凝望流年 2024-12-29 11:05:25
  1. 将 GlobalSuppressions.cs 添加到项目的根目录。

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

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

那有用吗?

  1. Add GlobalSuppressions.cs to root of project.

  2. Add your [module...

  3. Replace the word module with assembly.

Does that work?

向地狱狂奔 2024-12-29 11:05:25

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

明显的缺点是您必须在代码中添加这些...

It actually works. You can add a SupressMessageAttribute to the method that contains the expression. Just don't use RequiresAtCall. Instead, use 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);
}

The obvious downside is that you will have to plaster your code with these...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文