通过 TypeConverters 将 'anything' 转换为字符串
Converting 'anything' to string via TypeConverters
我正在尝试创建一个实用方法,它使用 TypeConverter
将任意类型的值转换为另一种类型。但是,当尝试将某些内容转换为字符串时,TypeConverter
总是失败。
private bool TryConvertType(object value, Type targetType, out object result) {
result = null;
if(value == null)
return false;
try {
if(targetType.IsInstanceOfType(value)) {
result = value;
return true;
}
var typeConverter = TypeDescriptor.GetConverter(targetType);
if(typeConverter.CanConvertFrom(value.GetType())) {
result = typeConverter.ConvertFrom(value);
return true;
}
...
给定 value
作为类型 bool
和 targetType
string
,方法 .CanConvertFrom()
总是 returns 错误。为什么不能从 boolean
值创建 string
?或者我在这里遗漏了什么?
我不知道确切原因,但查看 StringConverter
(here)
的来源
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) {
return true;
}
return base.CanConvertFrom(context, sourceType);
}
对于除字符串以外的任何类型,它都遵循基 TypeConverter
class;我根本无法让任何类型的 CanConvertFrom
到 return 为真!我不知道为什么会这样,但你总是可以这样做:
try
{
var convertedValue = Convert.ChangeType(value,targetType);
return convertedValue;
}
catch(FormatException fex)
{
//can't convert for whatever reason.
}
And as a bonus, I'll throw in the obligatory warning that wildly converting from one type to another may lead to explosions, there may be a better design to achieve your goal, etc, etc :)
您正在向后使用 TypeConverter
。或者更确切地说,您正在尝试在 bool
:
上使用 string
TypeConverter
var typeConverter = TypeDescriptor.GetConverter(targetType);
这为您提供了 TypeConverter
字符串,但您要转换的变量是 bool
。您还想使用 CanConvertTo
/ ConvertTo
进行实际转换:
// get bool type TypeConverter
var typeConverter = TypeDescriptor.GetConverter(value.GetType());
// can it convert a bool to string?
if (typeConverter.CanConvertTo(targetType))
{
// joy!
result = typeConverter.ConvertTo(value, targetType);
return true;
}
测试代码:
if (TryConvertType(b, t, out result))
Console.WriteLine(result.ToString());
输出:
True
当然,ToString()
已经可以处理几乎任何事情。
我正在尝试创建一个实用方法,它使用 TypeConverter
将任意类型的值转换为另一种类型。但是,当尝试将某些内容转换为字符串时,TypeConverter
总是失败。
private bool TryConvertType(object value, Type targetType, out object result) {
result = null;
if(value == null)
return false;
try {
if(targetType.IsInstanceOfType(value)) {
result = value;
return true;
}
var typeConverter = TypeDescriptor.GetConverter(targetType);
if(typeConverter.CanConvertFrom(value.GetType())) {
result = typeConverter.ConvertFrom(value);
return true;
}
...
给定 value
作为类型 bool
和 targetType
string
,方法 .CanConvertFrom()
总是 returns 错误。为什么不能从 boolean
值创建 string
?或者我在这里遗漏了什么?
我不知道确切原因,但查看 StringConverter
(here)
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string)) {
return true;
}
return base.CanConvertFrom(context, sourceType);
}
对于除字符串以外的任何类型,它都遵循基 TypeConverter
class;我根本无法让任何类型的 CanConvertFrom
到 return 为真!我不知道为什么会这样,但你总是可以这样做:
try
{
var convertedValue = Convert.ChangeType(value,targetType);
return convertedValue;
}
catch(FormatException fex)
{
//can't convert for whatever reason.
}
And as a bonus, I'll throw in the obligatory warning that wildly converting from one type to another may lead to explosions, there may be a better design to achieve your goal, etc, etc :)
您正在向后使用 TypeConverter
。或者更确切地说,您正在尝试在 bool
:
string
TypeConverter
var typeConverter = TypeDescriptor.GetConverter(targetType);
这为您提供了 TypeConverter
字符串,但您要转换的变量是 bool
。您还想使用 CanConvertTo
/ ConvertTo
进行实际转换:
// get bool type TypeConverter
var typeConverter = TypeDescriptor.GetConverter(value.GetType());
// can it convert a bool to string?
if (typeConverter.CanConvertTo(targetType))
{
// joy!
result = typeConverter.ConvertTo(value, targetType);
return true;
}
测试代码:
if (TryConvertType(b, t, out result))
Console.WriteLine(result.ToString());
输出:
True
当然,ToString()
已经可以处理几乎任何事情。