你可以在没有TypeConverterAttribute的情况下分配TypeConverter吗?

依赖性要求迫使我在不同的程序集中有一个类及其TypeConverter。

有没有办法在不使用TypeConverterAttribute的情况下将TypeConverter分配给类,从而导致循环程序集引用。

谢谢。

嗯,不确定我以前见过这个,但你可以在运行时使用TypeDescriptor添加TypeConverterAttribute,所以给出我的示例类:

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() { TypeDescriptor.AddAttributes(typeof(IType), new TypeConverterAttribute(typeof(IConverterType))); } AssignTypeConverter(); 

希望有所帮助。

您仍然可以使用TypeConverterAttribute并使用其构造函数来接受完全限定的名称。 请参阅MSDN 。