Vorrei implementare una funzione di conversione di tipo runtime per scopi generici che utilizza .Net TypeConverters per eseguire la conversione.Come cercare e richiamare un TypeConverter .Net per un tipo particolare?
Qualcuno sa come cercare e invocare un TypeConverter per un tipo particolare?
Si consideri questo esempio 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 usare 'dotnet' per un tag. –