Dynamically Modifying Attribute Properties
Is it possible to alter attribute parameters after an assembly has been loaded? Consider the following class:
public class UserInfo { [Category("change me!")] public int Age { get; set; } [Category("change me!")] public string Name { get; set; } }
Despite being a third-party vendor class (prohibiting code modification), you wish to modify the "change me" category name when binding an instance to a property grid.
Solution:
Attribute instance values can be dynamically modified at runtime. Obtain the attribute instance:
ASCII[] attrs1 = (ASCII[])typeof(MyClass).GetCustomAttributes(typeof(ASCII), false);
Modify its public variables:
attrs1[0].MyData = "A New String";
Create another instance to demonstrate the change:
ASCII[] attrs3 = (ASCII[])typeof(MyClass).GetCustomAttributes(typeof(ASCII), false); MessageBox.Show(attrs3[0].MyData);
Reference: http://www.vsj.co.uk/articles/display.asp?id=713
The above is the detailed content of Can You Dynamically Change Attribute Properties After Assembly Load?. For more information, please follow other related articles on the PHP Chinese website!