public object GetEditor(object component, Type editorBaseType) {
object editor = null;
// For components, the design time object for them may want to redefine the
// attributes. So, we search the attribute here based on the component. If found,
// we then search on the same attribute based on type. If the two don't match, then
// we cannot cache the value and must re-create every time. It is rare for a designer
// to override these attributes, so we want to be smart here.
//
AttributeCollection attrs;
EditorAttribute attr = null;
attrs = GetAttributes(component);
for (int i = 0; i < attrs.Count; i++) {
if (attrs[i] is EditorAttribute) {
EditorAttribute a = (EditorAttribute)attrs[i];
Type attrEditorBaseType = GetTypeFromName(a.EditorBaseTypeName);
if (attrEditorBaseType != null && attrEditorBaseType == editorBaseType) {
attr = a;
break;
}
}
}
if (attr != null) {
// Now, compare this attribute against the one provided by the normal attribute set for the
// type. If it is the same, then we can resort to the normal GetEditor call. Otherwise,
// we must create the editor anew.
//
EditorAttribute baseAttr = null;
attrs = GetAttributes(null);
for (int i = 0; i < attrs.Count; i++) {
if (attrs[i] is EditorAttribute) {
EditorAttribute a = (EditorAttribute)attrs[i];
Type attrEditorBaseType = GetTypeFromName(a.EditorBaseTypeName);
if (attrEditorBaseType != null && attrEditorBaseType == editorBaseType) {
baseAttr = a;
break;
}
}
}
if (attr != baseAttr) {
// The attribute we should use is a custom attribute provided by the
// designer of this object. Create the editor directly. This will
// be fairly rare (nothing in .NET uses this today, and we're pretty
// broad), so I'm not too concerned with caching this.
//
Type type = GetTypeFromName(attr.EditorTypeName);
if (type != null) {
editor = CreateInstance(type);
}
}
}
if (editor == null) {
editor = GetEditor(editorBaseType);
}
else {
// As a quick sanity check, check to see that the editor we got back is of
// the correct type.
//
if (editor != null && !editorBaseType.IsInstanceOfType(editor)) {
Debug.Fail("Editor " + editor.GetType().FullName + " is not an instance of " + editorBaseType.FullName + " but it is in that base types table.");
editor = null;
}
}
return editor;
}