You can't do this via config; but you can control attributes by writing a custom component-model implementation; i.e. writing your own PropertyDescriptor
, and using ICustomTypeDescriptor
or TypeDescriptionProvider
to associate it. Lots of work.
Update
I thought of a sneaky way to do it; see below, where we filter it to 2 of the properties using a string at runtime. If you don't own the type (to set the [TypeConverter]
), then you can use:
TypeDescriptor.AddAttributes(typeof(Test),
new TypeConverterAttribute(typeof(TestConverter)));
to associate the converter at runtime.
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System;
class TestConverter : ExpandableObjectConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, System.Attribute[] attributes)
{
PropertyDescriptorCollection orig = base.GetProperties(context, value, attributes);
List<PropertyDescriptor> list = new List<PropertyDescriptor>(orig.Count);
string propsToInclude = "Foo|Blop"; // from config
foreach (string propName in propsToInclude.Split('|'))
{
PropertyDescriptor prop = orig.Find(propName, true);
if (prop != null) list.Add(prop);
}
return new PropertyDescriptorCollection(list.ToArray());
}
}
[TypeConverter(typeof(TestConverter))]
class Test
{
public string Foo { get; set; }
public string Bar { get; set; }
public string Blop { get; set; }
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Test test = new Test { Foo = "foo", Bar = "bar", Blop = "blop"};
using (Form form = new Form())
using (PropertyGrid grid = new PropertyGrid())
{
grid.Dock = DockStyle.Fill;
form.Controls.Add(grid);
grid.SelectedObject = test;
Application.Run(form);
}
}
}