public TypeConverter GetConverter(object component) {
TypeConverter obj = 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.
//
TypeConverterAttribute attr = (TypeConverterAttribute)GetAttributes(component)[typeof(TypeConverterAttribute)];
if (attr != null) {
TypeConverterAttribute baseAttr = (TypeConverterAttribute)GetAttributes(null)[typeof(TypeConverterAttribute)];
if (baseAttr != attr) {
Type converterType = GetTypeFromName(attr.ConverterTypeName);
if (converterType != null && typeof(TypeConverter).IsAssignableFrom(converterType)) {
obj = (TypeConverter)CreateInstance(converterType);
}
}
}
// No custom attribute, so we can just use the stock one.
//
if (obj == null) {
obj = GetConverter();
}
return obj;
}