如何为通用闭源类型的所有实例指定自定义UITypeEditor?

时间:2012-10-25 09:12:59

标签: c# .net typedescriptor uitypeeditor

如何为通用类型的所有实例指定自定义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();

1 个答案:

答案 0 :(得分:0)

尝试

中的内容

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));