依赖性要求迫使我在不同的程序集中有一个类及其TypeConverter.
有没有办法在不使用TypeConverterAttribute的情况下将TypeConverter分配给类,从而导致循环程序集引用.
谢谢.
Mat*_*ott 19
嗯,不确定我以前见过这个,但你可以在运行时使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以有一个方法:
public void AssignTypeConverter<IType, IConverterType>()
{
TypeDescriptor.AddAttributes(typeof(IType), new TypeConverterAttribute(typeof(IConverterType)));
}
AssignTypeConverter<MyType, MyTypeConverter>();
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.