【问题标题】:IExtenderProvider add just some properties depending on object typeIExtenderProvider 根据对象类型仅添加一些属性
【发布时间】:2015-12-29 11:04:24
【问题描述】:

我遇到了一个问题,我不知道这是否确实可行(如果有“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)。

我的方法的问题在于,我在 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 的源代码,发生这种情况的原因“可以说”很清楚。它在内部创建了一个 TypeId,我怀疑是 WinForms 设计器正在使用的东西,如下所示:

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

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

我将测试派生 ProvidePropertyAttribute 并创建一个不同的 TypeId,因为它似乎可以覆盖,但我希望 winforms 设计师期望特定的 ProvidePropertyAttribute 类型而不是派生类型(winforms 设计师对这些东西很挑剔)。

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

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

【问题讨论】:

  • 您要扩展的控件是您的自定义控件吗?
  • @RezaAghaei 不,正如问题所述,它们是第三方(我没有源代码)

标签: c# winforms windows-forms-designer


【解决方案1】:

我知道这是一个快速的答案,但这几天让我发疯了,所以我走了一条看起来还不错的不同路线。

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

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

更新

作为记录,链接答案中提供的解决方案有效,但是它有一个问题,TypeDescriptionProvider 会被派生类拾取,但是返回的TypeDescriptor 只会返回基础对象的属性(您在父级TypeDescriptor 中传递的那个),从而在诸如winforms 设计器之类的东西中造成严重破坏。

我创建了一个通用的属性覆盖器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);
}

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2015-11-25
    • 2021-03-18
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    相关资源
    最近更新 更多