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

时间:2015-12-29 11:04:25

标签: c# winforms windows-forms-designer

我遇到了问题,我不知道这是否确实可行(如果有“hacky”方式,我会全力以赴,但我还没找到)。

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

这些控件不一定从相同的基础继承(如果它们存在,则基础不一定具有我想要扩展的属性,并且这些属性在同一个类中定义)。

因此,想象一下,例如,我想为它们的属性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)。

我的方法存在的问题是,我在扩展类型的所有中看到所有扩展属性。

说,如果AButton只有Image,我只想看到ImageCustom而不是SmallImageCustomLargeImageCustom等。

所以我的方法就是这样做:

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

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

现在问题是,如果我想在ImageCustomAButton中展示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设计师挑剔这些东西)。

哎呀,ProvidePropertyAttributesealed所以我无法推导并制作我的自定义TypeId,似乎(并非我非常希望这会起作用)

与此同时,任何人都曾经做过类似的事情并知道我可以使用的东西吗?

1 个答案:

答案 0 :(得分:1)

我知道这是一个快速的答案,但这让我疯了几天,所以我走了一条不同的路线似乎工作得很好。

由于目标目标(正如我在我的问题中所解释的)是在某些属性上更改UITypeEditor,因此我创建了一个覆盖属性的非可视组件(使用自定义TypeDescriptor )在这些属性上,并在那里指定我的自定义UITypeEditor

我使用this answer作为实现属性覆盖TypeDescriptor的基础。

更新

对于记录,链接答案中提供的解决方案有效,但是它有一个问题,TypeDescriptionProvider将被派生用于派生类,但返回的TypeDescriptor只会返回属性基础对象(您在父TypeDescriptor中传递的对象),导致像winforms设计者一样的havok。

我制作了一个通用的属性 - 覆盖者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;
        }
    }
    
  3. 这就是你如何使用它。我评论过这个:

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

    到目前为止,它已经创造了奇迹。