IExtenderProvider 根据对象类型仅添加一些属性

IExtenderProvider add just some properties depending on object type

我有一个问题,我不知道这是否确实可行(如果有 "hacky" 方法,我全力以赴,但我还没有找到)。

我有一个 IExtenderProvider 组件,我正在使用它来拥有自己的 UITypeEditor 第三方控件的某些属性(出于显而易见的原因,我无法更改)。

这些控件不一定继承自同一个基(即使继承,基也不一定具有我想要扩展的属性,这些属性是在同一个 class 中定义的)。

因此,假设我想为它们的属性 ImageGlyphLargeGlyphSmallGlyph 做一个替代 属性。

所以我有类似的东西:

[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)。

我的方法的问题是我在 all 扩展类型中看到 all 扩展属性。

说,如果AButton只有Image,我只想看ImageCustom,不想看SmallImageCustomLargeImageCustom

所以我的方法是这样做:

[ProvideProperty("LargeGlyphCustom", typeof (OtherControl))]
// other properties
[ProvideProperty("ImageCustom", typeof(AButton))]
public class MyImageExtender : Component, IExtenderProvider
// ...

这似乎工作正常,现在我只在 AButton 上看到 ImageCustom,在 OtherControl 上看到 LargeGlyphCustom

现在的问题是,如果我想在AButtonOtherControl中都显示ImageCustom,我曾想过这样做:

[ProvideProperty("ImageCustom", typeof(AButton))]
[ProvideProperty("ImageCustom", typeof(OtherControl))]
public class MyImageExtender : Component, IExtenderProvider

虽然这不起作用,我只能在 AButton 上看到 ImageCustom,但在 OtherControl 上看不到。

反编译 ProvidePropertyAttribute 的源代码,发生这种情况的原因 "arguably" 很清楚。它在内部创建了一个 TypeId,我 怀疑 是 WinForms 设计器使用的,如下所示:

public override object TypeId
{
  get
  {
    return (object) (this.GetType().FullName + this.propertyName);
  }
}

这使得TypeId为"ProvidePropertyAttributeImageCustom",因此无法区分不同的接收器类型。

我要测试推导 ProvidePropertyAttribute 并创建一个不同的 TypeId 因为它看起来可以覆盖,但我希望 winforms 设计器期望特定的 ProvidePropertyAttribute类型而不是派生类型(winforms 设计者对这些东西很挑剔)。

哎呀,ProvidePropertyAttributesealed,所以我无法推导和制作我的自定义 TypeId,看来(并不是我寄予厚望这会起作用)

同时,有没有人做过类似的事情并且知道我可以使用的东西?

我知道这是一个快速的回答,但这几天让我抓狂,所以我选择了另一条似乎工作正常的路线。

由于目标(正如我在问题中所解释的那样)是更改某些属性的 UITypeEditor,因此我制作了一个覆盖属性的 non-visual 组件(使用自定义 TypeDescriptor) 在这些属性上,并在那里分配我的自定义 UITypeEditor

我使用 this answer 作为实施 property-overriding TypeDescriptor 的基础。

更新

根据记录,链接答案中提供的解决方案有效,但是它有一个问题,即 TypeDescriptionProvider 会被派生 类 拾取,但是 returned TypeDescriptor 只会 return 基础对象的属性(您在父对象中传递的属性 TypeDescriptor),导致像 winforms 设计器这样的东西受到破坏。

我做了一个万能的 property-overrider TypeDescriptionProvider。到目前为止,它工作得很好。这是实现。请参阅 linked answer 以了解其来源的解释:

  1. 提供商:

    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;
        }
    }
    
  2. 这是实际的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);
}

现在,根据我的问题的要求,我创建了一个简单的 drop-in 组件,我将其放在基本表单上,它就是这样做的:

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.
    }
}

到目前为止,它产生了奇迹。