我想实现一个通用的运行时类型转换函数,它使用.Net TypeConverters来进行转换.
有谁知道如何查找和调用特定类型的TypeConverter?
考虑一下这个C#示例:
//
// 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 …Run Code Online (Sandbox Code Playgroud)