Domanda

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));
}
È stato utile?

Soluzione

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.

Altri suggerimenti

  1. Add GlobalSuppressions.cs to root of project.

  2. Add your [module...

  3. Replace the word module with assembly.

Does that work?

It actually works. You can add a SupressMessageAttribute to the method that contains the expression. Just don't use RequiresAtCall. Instead, use 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);
}

The obvious downside is that you will have to plaster your code with these...

Take a peek at the Build tab of your project properties. There is a "Suppress Warnings" field.

/nowarn (C# Compiler Options)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top