IExtenderProvider добавить только некоторые свойства в зависимости от типа объекта
У меня есть проблема, и я не знаю, действительно ли это выполнимо (если есть "хакерский" способ, я все готов, но я не нашел его).
у меня есть IExtenderProvider
компонент, который я использую, чтобы иметь свой собственный UITypeEditor
для некоторых свойств сторонних элементов управления (которые я не могу изменить по понятным причинам).
Эти элементы управления не обязательно наследуются от одной и той же базы (и если они есть, база не обязательно имеет свойства, которые я хочу расширить, и они определены в том же классе).
Итак, представьте, например, что я хочу сделать альтернативное свойство для свойств Image
, Glyph
, LargeGlyph
, SmallGlyph
на них.
Итак, у меня есть что-то вроде:
[ProvideProperty("LargeGlyphCustom", typeof (object))]
[ProvideProperty("GlyphCustom", typeof(object))]
[ProvideProperty("SmallImageCustom", typeof(object))]
[ProvideProperty("LargeImageCustom", typeof(object))]
[ProvideProperty("ImageCustom", typeof(object))]
public class MyImageExtender : Component, IExtenderProvider
{
private readonly Type[] _extendedTypes =
{
typeof (OtherControl),
typeof (SomeOtherControl),
typeof (AControl),
typeof (AButton)
};
bool IExtenderProvider.CanExtend(object o)
{
if (!DesignMode) return false;
return _extendedTypes.Any(t => t.IsInstanceOfType(o));
}
// Implement the property setter and getter methods
}
Все идет нормально. Я вижу свои свойства на элементах управления ожидаемых типов.
Тем не менее, это замены (просто чтобы изменить UITypeEditor
) свойств в контроле.
Проблема с моим подходом состоит в том, что я вижу все расширенные свойства во всех расширенных типах.
Скажи если AButton
только имеет Image
Я только хочу увидеть ImageCustom
и не SmallImageCustom
, LargeImageCustom
, так далее.
Так что мой подход был сделать это:
[ProvideProperty("LargeGlyphCustom", typeof (OtherControl))]
// other properties
[ProvideProperty("ImageCustom", typeof(AButton))]
public class MyImageExtender : Component, IExtenderProvider
// ...
Казалось, это работает нормально, и теперь я вижу только ImageCustom
на AButton
, а также LargeGlyphCustom
на OtherControl
,
Теперь проблема в том, если я хочу показать ImageCustom
в обоих AButton
а также OtherControl
Я думал сделать это:
[ProvideProperty("ImageCustom", typeof(AButton))]
[ProvideProperty("ImageCustom", typeof(OtherControl))]
public class MyImageExtender : Component, IExtenderProvider
Это не работает, хотя, я только вижу ImageCustom
на AButton
, но не на OtherControl
,
Декомпиляция источников для ProvidePropertyAttribute
причина, по которой это происходит, "возможно" ясна. Это внутренне создает TypeId
Я подозреваю, что дизайнер WinForms использует это так:
public override object TypeId
{
get
{
return (object) (this.GetType().FullName + this.propertyName);
}
}
Что делает TypeId "ProvidePropertyAttributeImageCustom"
, поэтому он не может различать разные типы приемников.
Я собираюсь проверить вывод ProvidePropertyAttribute
и создать другой TypeId
так как это кажется переопределимым, но я ожидаю, что дизайнер winforms ожидает конкретного ProvidePropertyAttribute
тип, а не производный (дизайнер winforms придирчив к этим вещам).
Ой, ProvidePropertyAttribute
является sealed
поэтому я не могу вывести и сделать свой заказ TypeId
кажется (не то чтобы я возлагал большие надежды на то, что это сработает вообще)
В то же время, кто-нибудь когда-либо делал что-то подобное и знает, что я мог бы использовать?
1 ответ
Я знаю, что это быстрый ответ, но это сводило меня с ума в течение нескольких дней, поэтому я пошел другим путем, который, кажется, работает очень хорошо.
Поскольку цель (как я объяснил в моем вопросе) состояла в том, чтобы изменить UITypeEditor
в некоторых свойствах я создал невизуальный компонент, который переопределяет атрибуты (используя TypeDescriptor
) на эти свойства, и назначьте мой пользовательский UITypeEditor
там.
Я использовал этот ответ в качестве основы для реализации переопределения свойств TypeDescriptor
,
Обновить
Для справки, решение, представленное в связанном ответе, сработало, однако возникла проблема, когда TypeDescriptionProvider
будет выбран для производных классов, однако возвращаемый TypeDescriptor
будет возвращать только свойства для базового объекта (тот, для которого вы передали в родительском объекте TypeDescriptor
), вызывая хаок в таких вещах, как конструктор winforms.
Я сделал универсальный переопределение собственности TypeDescriptionProvider
, До сих пор это работало просто отлично. Вот реализация. Смотрите связанный ответ для объяснения, откуда это взялось:
Провайдер:
internal class PropertyOverridingTypeDescriptionProvider : TypeDescriptionProvider { private readonly Dictionary<Type, ICustomTypeDescriptor> _descriptorCache = new Dictionary<Type, ICustomTypeDescriptor>(); private readonly Func<PropertyDescriptor, bool> _condition; private readonly Func<PropertyDescriptor, Type, PropertyDescriptor> _propertyCreator; public PropertyOverridingTypeDescriptionProvider(TypeDescriptionProvider parentProvider, Func<PropertyDescriptor, bool> condition, Func<PropertyDescriptor, Type, PropertyDescriptor> propertyCreator) : base(parentProvider) { _condition = condition; _propertyCreator = propertyCreator; } public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) { lock (_descriptorCache) { ICustomTypeDescriptor returnDescriptor; if (!_descriptorCache.TryGetValue(objectType, out returnDescriptor)) { returnDescriptor = CreateTypeDescriptor(objectType); } return returnDescriptor; } } private ICustomTypeDescriptor CreateTypeDescriptor(Type targetType) { var descriptor = base.GetTypeDescriptor(targetType, null); _descriptorCache.Add(targetType, descriptor); var ctd = new PropertyOverridingTypeDescriptor(descriptor, targetType, _condition, _propertyCreator); _descriptorCache[targetType] = ctd; return ctd; } }
Это актуальный
TypeDescriptor
:internal class PropertyOverridingTypeDescriptor : CustomTypeDescriptor { private readonly ICustomTypeDescriptor _parent; private readonly PropertyDescriptorCollection _propertyCollection; private readonly Type _objectType; private readonly Func<PropertyDescriptor, bool> _condition; private readonly Func<PropertyDescriptor, Type, PropertyDescriptor> _propertyCreator; public PropertyOverridingTypeDescriptor(ICustomTypeDescriptor parent, Type objectType, Func<PropertyDescriptor, bool> condition, Func<PropertyDescriptor, Type, PropertyDescriptor> propertyCreator) : base(parent) { _parent = parent; _objectType = objectType; _condition = condition; _propertyCreator = propertyCreator; _propertyCollection = BuildPropertyCollection(); } private PropertyDescriptorCollection BuildPropertyCollection() { var isChanged = false; var parentProperties = _parent.GetProperties(); var pdl = new PropertyDescriptor[parentProperties.Count]; var index = 0; foreach (var pd in parentProperties.OfType<PropertyDescriptor>()) { var pdReplaced = pd; if (_condition(pd)) { pdReplaced = _propertyCreator(pd, _objectType); } if (!ReferenceEquals(pdReplaced, pd)) isChanged = true; pdl[index++] = pdReplaced; } return !isChanged ? parentProperties : new PropertyDescriptorCollection(pdl); } public override object GetPropertyOwner(PropertyDescriptor pd) { var o = base.GetPropertyOwner(pd); return o ?? this; } public override PropertyDescriptorCollection GetProperties() { return _propertyCollection; } public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return _propertyCollection; } }
И вот как вы это используете. Я прокомментировал это:
private void ChangeTypeProperties(Type modifiedType, params string[] propertyNames)
{
// Get the current TypeDescriptionProvider
var curProvider = TypeDescriptor.GetProvider(modifiedType);
// Create a replacement provider, pass in the parent, this is important
var replaceProvider = new PropertyOverridingTypeDescriptionProvider(curProvider,
// This the predicate that says wether a `PropertyDescriptor` should be changed
// Here we are changing only the System.Drawing.Image properties,
// either those whose name we pass in, or all if we pass none
pd =>
typeof (System.Drawing.Image).IsAssignableFrom(pd.PropertyType) &&
(propertyNames.Length == 0 || propertyNames.Contains(pd.Name)),
// This our "replacer" function. It'll get the source PropertyDescriptor and the object type.
// You could use pd.ComponentType for the object type, but I've
// found it to fail under some circumstances, so I just pass it
// along
(pd, t) =>
{
// Get original attributes except the ones we want to change
var atts = pd.Attributes.OfType<Attribute>().Where(x => x.GetType() != typeof (EditorAttribute)).ToList();
// Add our own attributes
atts.Add(new EditorAttribute(typeof (MyOwnEditor), typeof (System.Drawing.Design.UITypeEditor)));
// Create the new PropertyDescriptor
return TypeDescriptor.CreateProperty(t, pd, atts.ToArray());
}
);
// Finally we replace the TypeDescriptionProvider
TypeDescriptor.AddProvider(replaceProvider, modifiedType);
}
Теперь, для требований моего вопроса, я создал простой вставляемый компонент, который я перетаскиваю на базовую форму, который делает именно это:
public class ToolbarImageEditorExtender : Component
{
private static bool _alreadyInitialized;
public ToolbarImageEditorExtender()
{
// no need to reinitialize if we drop more than one component
if (_alreadyInitialized)
return;
_alreadyInitialized = true;
// the ChangeTypeProperties function above. I just made a generic version
ChangeTypeProperties<OtherControl>(nameof(OtherControl.Glyph), nameof(OtherControl.LargeGlyph));
ChangeTypeProperties<AButton>(nameof(AButton.SmallImage), nameof(AButton.LargeImage));
// etc.
}
}
Пока что он творил чудеса.