Note: this article will mention a free tool “Enar Spy” for Enterprise Architect. Get more information here:

  • What are attributes?

In .NET attributes are used to combine code with declarations. e.g. is the class serializable, or is the property readonly.
Get more information here: codeproject

  • My Scenario:

In the internet you can find a couple of blogposts, dealing with setting attributes at runtime . I needed this to build a read-only property Grid, without modifying all the objects, I want to display.

Most information of this article is based on: dotnetfacts

In a short summary: the read-only attributes are set on all public properties except collections.  This is to keep the CollectionEditor button alive, which is disabled when setting readonly= true on collections too.

  • What I did:

You cannot create property-based attributes during runtime, but the readonly attribute exists for each property of your class by default. Even, if you do not add the Line “[ReadOnly(false)]”.
This is, why the following code-snippet works. It sets the readonly-attribute on each public property except collections.
change attributes at runtime

  • My Problem:

When assigning the class to the Property-Grid, all properties are displayed readonly. And next to collections you still have the button to investigate them.
But when opening the CollectionsEditor you might discover the following:

Although class “Connector” contains more properties than just a ToString-Method, it is treated like a string object.
For a tool like “Enar Spy” this is totally worthless, as you cannot investigate any collections.

  • Solution:

By accident, I found an article mentioning my problem in a single sentence:
“Add the readonly-attribute to your class’ properties declaration (= in source code). Because if you don’t, .NET will mix it up and do some crazy stuff.”
(Shame on me, I do not remember, where I found that hint!)

That’s why I ended up adding the line “[ReadOnly(false)]” to each property of each class I use in Enar Spy.

And guess what?
The propertyGrid’s collection editor now works like a charm:

This issue definitely needs some more investigation.

Does anybody have more information about this topic?

I appreciate each comment to this topic!