I have created a helper method that parses values from an XML node, but at this moment I have to pass in the name of the property to value has to be assigned to.
I know of the [CallerMemberName] attribute, but I wonder whether there is an attribute that can be used to automatically get the Property name the value will be assigned to.
For example:
Current situation
I have this extension method:
public static string GetStringValueOrDefault(this XmlNode node, string elementName) =>
node[elementName]?.InnerText ?? default;
Which is called like this:
PrinterSettings.PrinterName = printerSettingsElement.GetStringValueOrDefault(nameof(PrinterSettings.PrinterName))
Desired situation
The same extension method, with an attribute:
public static string GetStringValueOrDefault(this XmlNode node, [AssigneeName] string elementName) =>
node[elementName]?.InnerText ?? default;
I made up the [AssigneeName] attribute, but I guess you get the idea.
Which should be called like this, which is much cleaner:
PrinterSettings.PrinterName = printerSettingsElement.GetStringValueOrDefault()