Add class-level ReadOnly attribute to an object at runtime.

How to add Readonly attribute

The following code shows how to add readonly Attribute to an object at runtime [C#]

TypeDescriptor.AddAttributes(this.SelectedObject, new Attribute[] { new ReadOnlyAttribute(_readOnly) });

How to Remove Readonly attribute

If you want to remove the attribute, you have to store the TypeDescriptor­Provider and then use the RemoveProvider method as shows the following code. [C#]

// store the provider

TypeDescriptionProvider provider = TypeDescriptor.AddAttributes(this.SelectedObject,

new Attribute[] { new ReadOnlyAttribute(_readOnly) });

// remove the provider

TypeDescriptor.RemoveProvider(provider, this.SelectedObject); 

blog comments powered by Disqus