我想执行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));
}发布于 2012-01-12 23:59:45
在对ranomore进行了更多的调查之后,代码契约中似乎出现了一个缺陷。
通过n => n.Value访问的类具有一个泛型T Value属性。如果类被更改为非泛型类(带有object Value),则警告将消失。(带有object Value的泛型类也会发出警告)。
当然,这并不能回答最初的问题,但我认为这是不可能的。
发布于 2012-01-07 02:09:29
那有用吗?
发布于 2013-02-21 17:29:30
实际上很管用。可以将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);
}最明显的缺点是你必须用这些来填充你的代码。
https://stackoverflow.com/questions/8609110
复制相似问题