I have this code:
private int _someProperty;
public int SomeProperty
{
get => _someProperty;
set => SetProperty(ref _someProperty, value);
}
private void SetProperty<T>(ref T field, T value)
{
if (!field.Equals(value))
{
field = value;
}
}
I need the SetProperty<T>
because I need a set logic in a big class with lots of properties, and that logic applies to all of them.
The code works for the case of _someProperty
being a variable. But in my case, it does not work because I reference to another property of a nested class, so I get the compilation error: "a property or indexer may not be passed as an out or ref parameter".
As properties are methods under the hood, I tried something like:
private setProperty<T>(ref Action<T> property, T value) {
//meaningful code
}
But I get the compilatuion error anyhow.
Is there a direct way to pass a property by reference to directly solve this problem somehow?