Vorrei implementare il seguente suggerimento di CodeContracts:Posso usare SuppressMessage su un metodo framework?
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)
Ci si sente come dovrei essere in grado di utilizzare SupressMessage con l'attributo di destinazione per rendere questo accada. Tuttavia, poiché questo è un metodo di Framework, non sono sicuro.
//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.")]
Come faccio a sopprimere a livello globale questo avvertimento, in modo da non dover basale o sopprimere tutte le avvertenze 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'è un motivo che non si sta usando 'Contract.Assume'? Troppi avvenimenti? – porges
Abbiamo cercato di stare lontano dal contratto. Assume generalmente, ma sì, ci sono un bel po 'di eventi e continuiamo ad aggiungerne altri. –
Suppongo che il problema sia che i vari modi per acquisire Expressions/MethodInfos non assicurano che il risultato non sia nullo. Hai preso in considerazione l'utilizzo di alcuni metodi di wrapper come quelli forniti in: http://social.msdn.microsoft.com/Forums/en-NZ/codecontracts/thread/d8e2c2ad-de37-42ef-a854-02052d821975? In questo modo hai solo bisogno di "Assumere" in un posto, quindi l'utilizzo di "Assume" è ridotto al minimo. – porges