"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
John Allen <no_spam@_nospam.comwrote:
How are you applying attributes at runtime? As far as I'm aware,
whether or not an attribute is applied to something is a compile-time
decision, and can't be changed at runtime.
You can create your own "PropertyDescriptor" objects at runtime and take
complete control over your properties including their attributes
(augmenting
or overriding whatever you want). See
"PropertyDescriptor.CreateAttributeCollection() " and
"PropertyDescriptor.AttributesArray" for instance. Note that you can can
take control over all types in general using the
"TypeDescriptionProviderAttribute" (for 2.0 and later),
"ICustomTypeDescriptor" and (to some extent) "TypeConverterAttribute".
The
documentation to figure this all out is no picnic however.
Ah, I see - not like the normal way of applying attributes at all.
Yes, and I think the only time you typically have to resort to this is when
you need to get granular control over a "PropertyGrid" at runtime (there is
no other way AFAIK and the techniques are documented by both MSFT and others
of course). Figuring out all the details is a huge pain however (and it
takes an inordinate amount of code to implement ths simplest change). Is it
just me or are the .NET docs in general very weak?
>
If your property *starts off* read-only, is it greyed out in the
property grid? I wonder whether it's not noticing the change.
If I simply apply the read-only attribute to my customized "Size" property
then it grays it out but not its sub-properties ("Width" and "Height"). You
would think that it would but then again, my "Size" property is actually a
member of a larger object which I'm customizing the properties for (using
"PropertyDescriptor" objects). I see no reason why the read-only attribute
shouldn't be propagated to children however (without manual intervention
since again, it takes a lot of code to do this).