Je voudrais implémenter une fonction de conversion de type d'exécution à usage général qui utilise les convertisseurs de type .Net pour effectuer la conversion.Comment chercher et appeler un .Net TypeConverter pour un type particulier?
Est-ce que quelqu'un sait comment rechercher et invoquer un TypeConverter pour un type particulier?
Considérons cet exemple 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 n'utilisez pas 'dotnet' pour un tag. –