How to get the name of a property from within the property without using Lambda?

  • Thread starter Thread starter Richard.Haggard
  • Start date Start date
R

Richard.Haggard

Guest
I'm trying to come up with a pattern that can be used to get the name of a property from within the property's setter so that the same line of code can be used to record new values being asserted to properties in a program.

Here's an example of how it would be used. In the following code snippet there is a property named Message. Because this is a Prism application SetProperty() is used to transfer a value to a backing member. If the new value is different from the old value then a true is returned and in such a case the code calls a method that will record the new value to a log ile. The ClassName things comes from another property which uses reflection to acquire the class name. The property is, obviously, 'Message' and that's the bit that I want to change. As it stands, everytime that the WriteToLog line is added to a property's setter it has to be modified. I don't want to modify it. I want it to figure out what the name of the property is on its own. I can do this with a lambda expression but if I do that then that can break edit and continue.

public string Message
{
get { return _message; }
set
{
if (SetProperty(ref _message, value))
WriteToLog.WriteLine($"{ClassName}.Message: {0}",
value);
}
}
private string _message;

So, any ideas?




Richard Lewis Haggard

Continue reading...
 
Back
Top