Dav*_*ley 5 c# code-contracts 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)
Run Code Online (Sandbox Code Playgroud)
感觉我应该能够使用具有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.")]
Run Code Online (Sandbox Code Playgroud)
如何全局抑制此警告,因此我不必基线或抑制所有呼叫站点警告?
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));
}
Run Code Online (Sandbox Code Playgroud)
经过对 ranomore 的进一步调查,代码合约中似乎存在一个错误。
正在访问的类n => n.Value有一个通用的T Value属性。如果该类更改为非泛型类(带有object Value),则警告消失。(通用类object Value也会给出警告)。
当然,这并没有回答最初的问题,但我认为不可能做到这一点。
| 归档时间: |
|
| 查看次数: |
1424 次 |
| 最近记录: |