Detecting if a Setter of a property has been called

Asked

Viewed 79 times

2

Hello I wonder if there is a way to find out if the value of a property has changed.

Example

public partial class MinhaClasseExemplo
{
    public int Id { get; set; }
    public String Nome { get; set; }
    public String Telefone { get; set; }
}

var teste = new MinhaClasseExemplo();
teste.Nome = "Hiago";
teste.Telefone = null;

In the above example I define the class and instate the object, after which I assign the value to the Nome and to the Telefone (yes I put the null on purpose). I would like to know via Reflection which ones setters were called, in that case the Nome and Telefone and not the Id. There’s a way to do that with C#? I need something like this to make my routine generic.

Thank you.

  • 1

    Just as curiosity and informative, there is an interface called INotifyPropertyChanged, which is used to monitor changes in the state of an object and update clients (clients), linked (Binding clients) with the object, that a property has been changed.

  • @Gabrielheming top guy!! That already helped, what is boring is having to keep creating the private properties =/

  • @Gabrielheming guy thank you so much!!! Help and a lot, want to put as an answer? So I mark it as correct. Will help me a lot!

  • I ended up deleting the comment, but there’s the link: https://stackoverflow.com/a/1316417/1628790 . I was in doubt about the . NET core. Implement it and see the result.

2 answers

1

Good morning buddy,

I’ve been giving a thought and the way I found it was to make a property that will store these changes and a method to record this information on Set of each property.

public partial class MinhaClasseExemplo
{
  public int Id
  {
    get
    {
      return this.Id;
    }
    set
    {
      GravaLogPropAlterada(Id);
      this.Id = value;
    }
  }

  public List<string> PropriedadesAlteradas { get; private set; }  = new List<string>();

  public void GravaLogPropAlterada(object property)
  {
    PropriedadesAlteradas.Add((InfoOf(() => property).ToString()));
  }

  static PropertyInfo InfoOf<T>(Expression<Func<T>> ex)
  {
    return (PropertyInfo)((MemberExpression)ex.Body).Member;
  }
}

Would that be the idea.

I hope I gave you a light!

0

It will have to implement, obligatorily, in the Setter the evocation of a method for what you want.

As far as I know there is no way to automatically figure out if a value has been changed without specific implementation.

The example below is functional, may help you in your final solution:

public static List<(dynamic classe, string propriedade)> Propriedades { get; set; } =
    new List<(dynamic, string)>();

internal static void AdicionarPropriedade<T>(T classe, string propriedade) where T : class
{
    if (!Propriedades.Contains((classe, propriedade)))
        Propriedades.Add((classe, propriedade));
}

public partial class MinhaClasseExemplo
{
    private string _Nome = string.Empty;
    private string _Telefone = string.Empty;
    private int _Id = 0;

    protected void OnPropertyChanged(string name)
    {
        AdicionarPropriedade(this, name);
    }

    public int Id
    {
        get { return _Id; }
        set
        {
            _Id = value;
            OnPropertyChanged("Id");
        }
    }
    public string Nome
    {
        get { return _Nome; }
        set
        {
            _Nome = value;
            OnPropertyChanged("Nome");
        }
    }
    public string Telefone
    {
        get { return _Telefone; }
        set
        {
            _Telefone = value;
            OnPropertyChanged("Telefone");
        }
    }
}

The list Propriedades will contain tuples with the class instance and the name of the changed properties. The class will be important if you want to implement something more generic with various class types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.