Me gustaría implementar una función de conversión de tipo de tiempo de ejecución de uso general que utilice .Net TypeConverters para hacer la conversión.¿Cómo buscar e invocar un .NET TypeConverter para un tipo particular?
¿Alguien sabe cómo buscar e invocar un TypeConverter para un tipo particular?
Considere este ejemplo de 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 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 't use' dotnet 'para una etiqueta. –