Ответ 1
Хм, не уверен, что я видел это раньше, но вы можете добавить TypeConverterAttribute во время выполнения с помощью TypeDescriptor, поэтому дайте мои примеры классов:
public class MyType
{
public string Name;
}
public class MyTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
return new MyType() { Name = (string) value };
return base.ConvertFrom(context, culture, value);
}
}
Тогда у меня мог бы быть метод:
public void AssignTypeConverter<IType, IConverterType>()
{
TypeDescriptor.AddAttributes(typeof(IType), new TypeConverterAttribute(typeof(IConverterType)));
}
AssignTypeConverter<MyType, MyTypeConverter>();
Надеюсь, что это поможет.