I can supply this class to the Properties control
I assume you mean PropertyGrid? If not, then let me know ;-p
I would like to be able to programmatically
control at runtime which properties are displayed.
To get past the default filters, you can actually create a custom tab
for the PropertyGrid (and remove the 2 that it provides) - there is a
full example of this on MSDN. You can then override GetProperties() to
do whatever you want and apply whatever filters you like to the
properties. I can't find the MSDN example, but there is one on
CodeProject that covers lots of other things too:
http://www.codeproject.com/cs/miscct...ExWinForms.asp
Is there a way to programmatically change a
property's attributes at at runtime?
Do you really mean a properties attributes? i.e. the "my description"
below:
[Description("my description")]
public string Name {get {return "Fred";}}
If so, then yes and no... although you can often tweak the attributes
on a PropertyDescriptor, the default reflective view on the component-
model will not respect this and you are likely to get the originals
back the next time you ask for them (since the original are burnt into
the class model). To do what you want (with full control - i.e. add/
remove attributes, work with any attribute) you probably need to
implement a custom PropertyDescriptor with bespoke (static?) storage
of the attributes. This would involve TypeDescriptionProvider (best
option) or ICustomTypeDescriptor (not quite as powerful). However,
this approach is not for the faint hearted, and you *really* need to
understand the System.ComponentModel before setting out...
If you only want to adjust a handful of attribute *values*, then
*sometimes* you can use a few tricks - i.e. I'm confident I could get
[Description], [DisplayName], [Category] all working [for edit, not
add/remove] by subclassing the attribute and providing bespoke storage
- similar to how MS manage i18n on these attributes.
Of course, to display and edit them in the PropertyGrid would need
another tab, with some custom virtual PropertyDescriptor entries that
write back to the entries... yet again, not for the faint hearted, but
it can be done.
Marc