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);
, , , , , ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Share to Facebook Share to Twitter Stumble It Share on Tumblr Digg More...