Chciałbym zaimplementować funkcję konwersji typu ogólnego wykonywania, która używa .Net TypeConverters do konwersji.Jak wyszukiwać i wywoływać .Net TypeConverter dla określonego typu?
Czy ktoś wie, jak wyszukać i wywołać TypeConverter dla określonego typu?
Rozważmy C# przykład:
//
// 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 do tagu użyj 'dotnet'. –