How to set ReadOnly attribute of some property of one instance not one class

A

Alan

Hi all,
I have one class including one collection. I use propertygrid to display and
edit its properties.
I'm trying to use reflection to change the status of one element to be
readonly, for example the first element in this collection.The result is
that all of elements changed. I guess the ReadOnly attribute is for class(or
type), not for instance.
How could i only change the status of one element of one collection?

Thanks in advance.

Alan
 
B

Branimir Giurov

I don't think that is a good idea.
You should manage the state of the elements at your get/set modifiers.
Like - throwing a custom exception when it's the first element in the
collection.

Cheers,
Branimir
 
M

Marc Scheuner [MVP ADSI]

I have one class including one collection. I use propertygrid to display and
edit its properties.
I'm trying to use reflection to change the status of one element to be
readonly, for example the first element in this collection.The result is
that all of elements changed. I guess the ReadOnly attribute is for class(or
type), not for instance.
How could i only change the status of one element of one collection?

In that case, you'd have to have a private boolean flag in the class,
which would indicate whether or not your special property is
read-only, or not.

In the Set accessor for that property, you'd need to check for that
flag, and if it's true, you'd simply discard / disregard the value
being set.

I don't think there's another way to do this, since as you say, the
property's status is otherwise a function of the CLASS, not the
instance....

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top