vendredi 7 avril 2023

How to trigger a callback when field is changed using reflection in c#

For example I want to log the Name value every time it changes.

using System;

class Data {
    public string Name;
    
    public Data(string name) {
        Name = name;
    }
}

public class Test
{
    public static void Main()
    {
        Data data = new("foo");
        
        object name = typeof(Data).GetField("Name").GetValue(data);
        
        Console.WriteLine(name);
    }
}




Aucun commentaire:

Enregistrer un commentaire