TypeConverter类型转换器中有2重写方法
1>canConvertFrom()
2>canConvertTo()
问题:感觉很搞
加入我有一个复杂类型
private Scope _scope = new Scope(); [Browsable(true)]
[TypeConverter(typeof(ScopeType))]
public Scope Scope
{
get
{
return _scope;
}
set
{
_scope = value;
}
}//CanConvertFrom重写方法
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(String)) return true; return base.CanConvertFrom(context, sourceType);
}
//CanConvertTo重写方法
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(String)) return true; if (destinationType == typeof(InstanceDescriptor)) return true; return base.CanConvertTo(context, destinationType);
}
我有点晕了,那个sourceType 和 destinationType 到底哪个指的是我的Scope类
据官方说是sourceType是要转换为转换器的类型
而 destinationType是将转换器对象转换为指定的类型
那我就搞不懂了sourceType是哪里来的 destinationType又是哪里来的,还有我的Scope复杂类型又和这2这之间有什么关联,不要太官方的话,我只要搞懂,谁能让我明白我100分全给他!谢谢!
1>canConvertFrom()
2>canConvertTo()
问题:感觉很搞
加入我有一个复杂类型
private Scope _scope = new Scope(); [Browsable(true)]
[TypeConverter(typeof(ScopeType))]
public Scope Scope
{
get
{
return _scope;
}
set
{
_scope = value;
}
}//CanConvertFrom重写方法
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(String)) return true; return base.CanConvertFrom(context, sourceType);
}
//CanConvertTo重写方法
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(String)) return true; if (destinationType == typeof(InstanceDescriptor)) return true; return base.CanConvertTo(context, destinationType);
}
我有点晕了,那个sourceType 和 destinationType 到底哪个指的是我的Scope类
据官方说是sourceType是要转换为转换器的类型
而 destinationType是将转换器对象转换为指定的类型
那我就搞不懂了sourceType是哪里来的 destinationType又是哪里来的,还有我的Scope复杂类型又和这2这之间有什么关联,不要太官方的话,我只要搞懂,谁能让我明白我100分全给他!谢谢!
对于to你的是 destinationType
对于to你的是 destinationType
你的意思是不管是Form还是To都表示我要转换的复杂类型Scope?
to是将你的类型转为 destinationType
bool b = TypeDescriptor.GetConverter(typeof(Person)).CanConvertTo(typeof(string));
// true 表示可以将Person转换为目标类型string
bool b2 = TypeDescriptor.GetConverter(typeof(Person)).CanConvertFrom(typeof(string));
// flase 表示要将string转换为当前转换器的类型,转换失败