Event after get property in C#

Viewed 33

I need event after get property.

For example I want something like this

public class ModelClass
{
    public string Title { get; set; }
}

OnBeforGet Function

public void OnBeforGet()
{
    Title = "Value";
}

Use Prop

public void Test()
{
    using (var modelClass = new ModelClass())
    {
        var title = modelClass.Title;
    }
}
1 Answers

I think you are looking for something like this. every call of 'Title' prop the 'OnBeforGet()' function will be fired

public class ModelClass
{
    private string _title;
    public string Title
    {
        get
        {
            OnBeforGet();
            return _title;
        }
        set
        {
            this._title = value;
        }
    }

    public void OnBeforGet()
    {
        Title = "value Updated Before Get function";
    }
}
Related