【问题标题】:How to specify a custom UITypeEditor for all instances of a generic closed-source type?如何为通用闭源类型的所有实例指定自定义 UITypeEditor?
【发布时间】:2012-10-25 09:12:59
【问题描述】:

如何为 generic 类型的所有实例指定自定义 UI 编辑器?该类型是在另一个我不拥有的程序集中定义的。


这是我尝试过的,受https://stackoverflow.com/a/849778/284795 启发,但没有任何效果(旧编辑器仍然存在)。在这里,泛型类型是List<> 和自定义编辑器DateTimeeditor - 废话,但这只是一个示例。

TypeDescriptor.AddAttributes(typeof(List<>),new EditorAttribute(typeof(System.ComponentModel.Design.DateTimeEditor),typeof(UITypeEditor)));
TypeDescriptor.GetEditor(new List<int>(),typeof(UITypeEditor)).Dump();

【问题讨论】:

  • 只需从该类型派生您自己的类,这样您就可以给它自己的 [Editor] 属性。
  • 不要认为这是可能的。 TypeDescriptor 解析机制基于类型字典,没有可以挂钩的事件。

标签: c# .net typedescriptor uitypeeditor


【解决方案1】:

尝试一下

Customer 是任意类

EditorBase 定义为

public class EditorBase : System.Drawing.Design.UITypeEditor {
}

代码:

System.Collections.Hashtable table = new System.Collections.Hashtable();

table.Add(typeof(Customer), typeof(EditorBase).AssemblyQualifiedName);

System.ComponentModel.TypeDescriptor.AddEditorTable(typeof(System.Drawing.Design.UITypeEditor), table);

object editor = System.ComponentModel.TypeDescriptor.GetEditor(typeof(Customer), typeof(System.Drawing.Design.UITypeEditor));

【讨论】:

    猜你喜欢
    • 2010-10-25
    • 2011-05-03
    • 2013-11-25
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多