Set a property to read-only or browsable to false custom attribute at runtime using System.Reflection
We had a problem recently where we wanted to set certain properties in a C# .NET class to read-only at runtime depending on the overarching product accessing the library so that it could be displayed in a property grid. Our new product XIA Configuration Express would have certain features that would be unavailable due to the nature of the product compared to the main XIA Configuration Server product. Custom attributes are designed to be assigned in the source code and not modified later so this requires some additional work with reflection to access the fields of the attribute or to add a new attribute to the collection if it doesn't already exist. NOTE: This changes the read-only attribute for the class not an instance of the class so if you cannot have the property being read-only for a single instance of the class and not of another instance of the class. /// <summary> /// Sets the readonly attribute value for the specified property. /// </summary>