2

I'd like to run a method on property changed. I'd like my code to compile to something like this:

public string Property
    {
        get { return _property; }
        set
        {
            _property= value;
            IWantToCallFromHere(); // I want to inject this call
            NotifyPropertyChanged();
        }
    }
Lennart
  • 9,657
  • 16
  • 68
  • 84
user963935
  • 493
  • 4
  • 20
  • This code have to be _generated_ by foddy from `public string Property { get; set; }` – user963935 Feb 18 '16 at 10:55
  • 1
    I just read about Fody. Let me help to understand it. If we just write Public property with get set, then compiler automatically generate code for us ? If I want to perform any logic in my Set property then where will I write using Fody ? Can we edit code that auto generated code by Fody ? – Ajay Sharma Aug 16 '16 at 10:14

1 Answers1

1

This is described in the Wiki in the page named On_PropertyName_Changed.

Essentially you add a method with the naming convention private void OnYourPropertyNameChanged()

A complete example of what you would like to achieve is as follows:

public string Property
{
    get; set;
}

private void OnPropertyChanged()
{
    IWantToCallFromHere();
}

which gets translated into

private string _property;
public string Property
{
    get => _property; 
    set
    {
        if(_property != value)
        {
            _property = value;
            OnPropertyChanged();
            NotifyPropertyChanged();
        }
    }
}

private void OnPropertyChanged()
{
    IWantToCallFromHere();
}
Erik Ovegård
  • 328
  • 2
  • 10