我可以在框架方法上使用 SuppressMessage 吗?
我想实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过对 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 genericT Value
property. If the class is changed to a non-generic class (withobject Value
) the warning disappears. (A generic class withobject 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.
将 GlobalSuppressions.cs 添加到项目的根目录。
添加您的[模块...
将单词模块替换为程序集。
那有用吗?
Add GlobalSuppressions.cs to root of project.
Add your [module...
Replace the word module with assembly.
Does that work?
它确实有效。您可以将
SupressMessageAttribute
添加到包含表达式的方法。只是不要使用RequiresAtCall
。相反,使用Requires
:明显的缺点是您必须在代码中添加这些...
It actually works. You can add a
SupressMessageAttribute
to the method that contains the expression. Just don't useRequiresAtCall
. Instead, useRequires
:The obvious downside is that you will have to plaster your code with these...