Domanda

I am currently working on a project where we would like to use the Fody PropertyChanged IL weaver. It appears to work fantastically, but it is doing strange things to our FxCop analysis built into visual studio.

We have CA1062 and CA2214 enabled: The former validates arguments of public methods and the latter checks for calls to overridable methods in constructors.

The second one, we have figured out is due to the fact that we are also using Caliburn.Micro which supplies a method called NotifyOfPropertyChanged which is virtual. This is a simple fix of changing the method to not be virtual.

The first issue is much harder and seemingly random. We have been hard core about validating our arguments from the get go and did not have any FxCop analysis errors before Fody, but when I added it through NuGet FxCop was seeing changes in places that shouldn't have any weaved code for property changed. I even looked at a diff of the de-compiled source before and after Fody, and there are FxCop conflicts in places where methods weren't changed at all.

So my question is whether I can somehow postpone Fody weaving till after the analysis is finished, or if there is some other way to get Fody and FxCop to interact nicely?

Thanks in advance

È stato utile?

Soluzione

Example:

public class Base : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Descendent : Base
{
    public Descendent()
    {
        DetermineDefaultMyPropertyValue();
    }

    public string MyProperty { get; set; }

    private void DetermineDefaultMyPropertyValue()
    {
        MyProperty = "Default value"; 
    }

    protected override void OnPropertyChanged(string propertyName)
    {
        // some property changed logic
    }
}

It could be solved by changing the auto implemented property to use a backing field. ( Only the ones that are set while constructing the object. )

public class Descendent : Base
{
    public Descendent()
    {
        DetermineDefaultMyPropertyValue();
    }

    private string _myProperty;
    public string MyProperty {
        get { return _myProperty; }
        set { _myProperty = value; }
    }

    private void DetermineDefaultMyPropertyValue()
    {
        _myProperty = "Default value"; 
    }

    protected override void OnPropertyChanged(string propertyName)
    {
        // some property changed logic
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top