Tôi muốn thực hiện các gợi ý sau đây từ CodeContracts:Tôi có thể sử dụng SuppressMessage trên phương pháp khung không?
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)
Nó cảm thấy như tôi sẽ có thể sử dụng SupressMessage với thuộc tính mục tiêu để thực hiện điều này. Tuy nhiên, vì đây là phương pháp Framework, tôi không chắc chắn.
//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.")]
Làm thế nào tôi có thể toàn cầu ngăn chặn cảnh báo này, vì vậy tôi không cần phải ban đầu hoặc ngăn chặn tất cả các cảnh báo callsite?
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));
}
Có một lý do mà bạn không sử dụng 'Contract.Assume'? Chỉ có quá nhiều lần xuất hiện? – porges
Chúng tôi đã cố gắng tránh xa Contract.Assume nói chung, nhưng có, có một vài lần xuất hiện và chúng tôi tiếp tục bổ sung thêm. –
Tôi đoán rằng vấn đề sau đó là các cách khác nhau để có được Expressions/MethodInfos không 'Đảm bảo 'rằng kết quả là không null. Bạn đã cân nhắc sử dụng một số phương pháp bao bọc như những phương pháp được đưa ra trong: http://social.msdn.microsoft.com/Forums/en-NZ/codecontracts/thread/d8e2c2ad-de37-42ef-a854-02052d821975? Bằng cách đó bạn chỉ cần 'Giả sử' ở một nơi, do đó, mức sử dụng' Giả sử' của bạn được giảm thiểu. – porges