One way to achieve this is by creating a custom attribute and applying it to the properties that need to be replaced. Here's an example implementation:
main.cs556 chars21 lines
This ReplaceValueAttribute
takes a Dictionary<string, string>
in its constructor, which contains the original and replacement values. The attribute also has a ReplaceValue
method, which takes the original value and returns the replaced value from the dictionary.
To apply this attribute to a property, you can use reflection as follows:
main.cs721 chars23 lines
This code creates a new instance of MyClass
, retrieves its MyProperty
using reflection, and applies the ReplaceValueAttribute
to it. It then retrieves the original value of the property, uses the attribute to replace it, and sets the property with the replaced value. Finally, it prints out the value of MyProperty
, which should be the replaced value.
gistlibby LogSnag