В отличие от других, это вполне возможно, и к тому же не так сложно. Например, вы хотите добавить некоторые новые атрибуты к некоторым свойствам, которые можно выбрать во время выполнения на основе некоторых критериев.
Для этого нам понадобятся два вспомогательных класса.
Сначала идет PropertyOverridingTypeDescriptor
, он позволяет нам предоставить наши собственные дескрипторы свойств для некоторых свойств, не затрагивая другие:
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));
}
}
Несколько замечаний:
- Конструктор принимает
ICustomTypeDescriptor
, здесь не о чем беспокоиться, мы можем получить его для любого типа или его экземпляр с TypeDescriptor.GetProvider(_settings).GetTypeDescriptor(_settings)
, где _settings может быть либо Type
, либо object
этого типа.
OverrideProperty
делает именно то, что нам нужно, подробнее об этом позже.
Другой нам нужен класс TypeDescriptionProvider
, который будет возвращать наш дескриптор настраиваемого типа вместо стандартного. Вот:
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;
}
}
Довольно просто: вы просто предоставляете экземпляр дескриптора типа при построении, и готово.
И наконец, обработка кода. Например, мы хотим, чтобы все свойства, оканчивающиеся на ConnectionString
в нашем объекте (или типе) _settings
, были доступны для редактирования с помощью System.Web.UI.Design.ConnectionStringEditor
. Для этого мы можем использовать этот код:
// 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);
Вот и все, теперь все свойства, заканчивающиеся на ConnectionString
, будут доступны для редактирования через ConnectionStringEditor
.
Как видите, мы просто каждый раз переопределяем некоторые функции реализации по умолчанию, поэтому система должна быть достаточно стабильной и вести себя должным образом.
person
Community
schedule
27.08.2012