Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use SuppressMessage on a framework method?

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));
}
like image 496
Dave Neeley Avatar asked Oct 24 '22 14:10

Dave Neeley


1 Answers

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 generic T Value property. If the class is changed to a non-generic class (with object Value) the warning disappears. (A generic class with object 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.

like image 85
porges Avatar answered Nov 12 '22 23:11

porges