I want to create an attribute that will allow me to specify some method applied to the property, for example:
public class MyClass
{
[MyAttribute(Converter="ConverterMethod")]
public string Prop { get; set; }
public static string ConverterMethod(string src)
{
return src + " converted";
}
}
What is the “right” way to do this?
This is how I see:
- Make a string property and extract the appropriate method with reflection at runtime
- Make
Dictionary<string, Func<string, string>>and fill it with appropriate methods at runtime. Then retrieve the method using the attribute string property as the key. This method is more resistant to refactoring, if I rename the method, everything will work (the dictionary key will remain the same, though) - IConverter
typeof(ConverterImpl). . , Type , .
? ? ?