0

I'm having trouble getting Fody & PropertyChanged working. I've got a .NET 4.7.2 project with both Fody(6.6.4) and PropertyChanged.Fody (4.1.0) packages added through NuGet, but only "PropertyChanged" shows in the solution explorer.

My example class that implements the INotifyPropertyChanged interface and a generic test:

[AddINotifyPropertyChangedInterface]
public class NotifyingClass: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Prop1{ get; set; }
}

public class MyClass
{
    public NotifyingClass notifying {get; set; }
    private bool hasChanges = false;

    public MyForm()
    {
        InitializeComponent();
        var test = new NotifyingClass();
        test.PropertyChanged += (sender, args){
            hasChanges = true; //Never gets called
        }

        test.Prop1 = "new value";

        if(!hasChanges) throw new Exception("PropertyChanged did not fire");
    }
}

I can't to get the PropertyChange event to fire. I tried decompiling with JustDecompile and dotPeek and they just show the same above code. I also tried using the [OnChangedMethod("SomethingChanged")] attribute to no avail. It seems like Fody isn't weaving the PropertyChanged functionality. My FodyWeavers.xml is:

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
  <PropertyChanged />
</Weavers>

My .csproj has multiple references to Fody-

    <Import Project="packages\PropertyChanged.Fody.4.1.0\build\PropertyChanged.Fody.props" Condition="Exists('packages\PropertyChanged.Fody.4.1.0\build\PropertyChanged.Fody.props')" />

    <Content Include="FodyWeavers.xml" />

    <Reference Include="PropertyChanged, Version=4.1.0.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
      <HintPath>packages\PropertyChanged.Fody.4.1.0\lib\net40\PropertyChanged.dll</HintPath>
    </Reference>

Is there supposed to be a reference also to Fody itself and not just PropertyChanged.Fody?

AMG
  • 139
  • 13
  • Tried your code and it works as expected. – Paweł Łukasik Mar 09 '23 at 17:43
  • Yeah, I thought it should work. My event handler just never fires. Is there a way to verify that Fody actually did what it's supposed to do? I tried decompiling it to see if there was anything added to the view model; it looks the same. From my limited understanding of Fody, the decompiled source should look different than the original source code. – AMG Mar 10 '23 at 21:27
  • Yes, the extra code should be inside `Prop1` when you decompile the binary – Paweł Łukasik Mar 10 '23 at 21:59
  • It seems to have resolved itself... I have no idea how, it just started working after abandoning this problem for the time being and working on other features. Shame I have no idea what changed. – AMG Apr 06 '23 at 18:42

0 Answers0