问题
I want to add some custom PropertyGrid-centric Attributes to the object\'s properties, to provide richer editing, hide some values and group them in categories, because that class I\'m working with doesn\'t provide such functionality and I can\'t do anything about it.
Really, it\'s for MS\'s Application Settings that generates code, so you can\'t extend it in any way property-wise. See my other question: Runtime AppSettings.settings editor dialog
回答1:
Unlike others have suggested, it's quite possible, and also not that hard. For example, you want to add some new attributes to some properties, which you can select at runtime based on some criteria.
There're two helper classes we'll need to implement this.
First goes PropertyOverridingTypeDescriptor
, it allows us to supply our own property descriptors for some properties, while keeping others intact:
public class PropertyOverridingTypeDescriptor : CustomTypeDescriptor
{
private readonly Dictionary<string, PropertyDescriptor> overridePds = new Dictionary<string, PropertyDescriptor>();
public PropertyOverridingTypeDescriptor(ICustomTypeDescriptor parent)
: base(parent)
{ }
public void OverrideProperty(PropertyDescriptor pd)
{
overridePds[pd.Name] = pd;
}
public override object GetPropertyOwner(PropertyDescriptor pd)
{
object o = base.GetPropertyOwner(pd);
if (o == null)
{
return this;
}
return o;
}
public PropertyDescriptorCollection GetPropertiesImpl(PropertyDescriptorCollection pdc)
{
List<PropertyDescriptor> pdl = new List<PropertyDescriptor>(pdc.Count+1);
foreach (PropertyDescriptor pd in pdc)
{
if (overridePds.ContainsKey(pd.Name))
{
pdl.Add(overridePds[pd.Name]);
}
else
{
pdl.Add(pd);
}
}
PropertyDescriptorCollection ret = new PropertyDescriptorCollection(pdl.ToArray());
return ret;
}
public override PropertyDescriptorCollection GetProperties()
{
return GetPropertiesImpl(base.GetProperties());
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetPropertiesImpl(base.GetProperties(attributes));
}
}
Few remarks:
- Constructor takes
ICustomTypeDescriptor
, no worries here, we can get one for any type or it's instance with theTypeDescriptor.GetProvider(_settings).GetTypeDescriptor(_settings)
where _settings can be eitherType
orobject
of that type. OverrideProperty
does just what we need, more on it later.
The other class we need is the TypeDescriptionProvider
that will return our custom type descriptor instead of the default one. Here it is:
public class TypeDescriptorOverridingProvider : TypeDescriptionProvider
{
private readonly ICustomTypeDescriptor ctd;
public TypeDescriptorOverridingProvider(ICustomTypeDescriptor ctd)
{
this.ctd = ctd;
}
public override ICustomTypeDescriptor GetTypeDescriptor (Type objectType, object instance)
{
return ctd;
}
}
Fairly simple: you just supply the type descriptor instance on construction and here you go.
And finally, processing code. For example, we want all properties ending with ConnectionString
in our object (or type) _settings
to be editable with the System.Web.UI.Design.ConnectionStringEditor
. To achieve that, we can use this code:
// prepare our property overriding type descriptor
PropertyOverridingTypeDescriptor ctd = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(_settings).GetTypeDescriptor(_settings));
// iterate through properies in the supplied object/type
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(_settings))
{
// for every property that complies to our criteria
if (pd.Name.EndsWith("ConnectionString"))
{
// we first construct the custom PropertyDescriptor with the TypeDescriptor's
// built-in capabilities
PropertyDescriptor pd2 =
TypeDescriptor.CreateProperty(
_settings.GetType(), // or just _settings, if it's already a type
pd, // base property descriptor to which we want to add attributes
// The PropertyDescriptor which we'll get will just wrap that
// base one returning attributes we need.
new EditorAttribute( // the attribute in question
typeof (System.Web.UI.Design.ConnectionStringEditor),
typeof (System.Drawing.Design.UITypeEditor)
)
// this method really can take as many attributes as you like,
// not just one
);
// and then we tell our new PropertyOverridingTypeDescriptor to override that property
ctd.OverrideProperty(pd2);
}
}
// then we add new descriptor provider that will return our descriptor instead of default
TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(ctd), _settings);
That's it, now all properties ending with ConnectionString
will be editable through ConnectionStringEditor
.
As you can see, we just override some functionality of the default implementation every time, so the system should be fairly stable and behave as expected.
回答2:
If you need to add attributes like [ExpandableObject] or [Editor] to properties of an object which class you can't edit you can add the attributes to the type of the property. So you can use reflection to inspect the object and use
TypeDescriptor.AddAttributes(typeof (*YourType*), new ExpandableObjectAttribute());
Then it behaves like you decorated all the properties of type YourType with the attribute.
回答3:
If you want rich custom PropertyGrid, an alternative design is to make your wrap your type in a class inheriting from CustomTypeDescriptor. You can then override GetProperties, annotating the properties of the underlying class with the attributes needed for PropertyGrid.
Detailed description in answer to a related question https://stackoverflow.com/a/12586865/284795
回答4:
The accepted answer does work, but it has a flaw: if you assign the provider to a base class, it'll also work for derived classes, however, since the PropertyOverridingTypeDescriptor
parent (from which it'll get its properties) is for the base type, the derived type will only find the base class properties. This causes havok in e.g., the winforms designer (and may cause you to lose data if you are using the TypeDescriptor
for serializing the data).
Just for the record, I've made a generic solution based on @Gman's answer, and I've posted it here as a solution to my own question (which was a different question, although the solution worked using this one).
来源:https://stackoverflow.com/questions/12143650/how-to-add-property-level-attribute-to-the-typedescriptor-at-runtime