Tôi muốn triển khai chức năng chuyển đổi loại thời gian chạy theo mục đích chung sử dụng .Net TypeConverters để thực hiện chuyển đổi.Cách tra cứu và gọi .Net TypeConverter cho một loại cụ thể?
Có ai biết cách tìm kiếm và gọi một TypeConverter cho một loại cụ thể không?
Xem xét việc này C# Ví dụ:
//
// Convert obj to the type specified by 'toType'.
//
object ConvertTo(object obj, Type toType)
{
if (TypeIsEqualOrDerivesFrom(obj.GetType(), toType)) <-- I know how to implement this.
{
// The type of obj is the same as the type specified by 'toType' or
// the type of obj derives from the type specified by 'toType'.
return obj;
}
if (TypeConverterExists(obj.GetType(), toType) <-- How do I implement this?
{
// There exists a type convertor that is capable of converting from
// the type of obj to the type specified by 'toType'.
return InvokeTypeConverter(obj, toType); <-- How do I implement this?
}
throw new TypeConversionFailedException();
}
Don không sử dụng 'dotnet' cho thẻ. –