Ich möchte eine universelle Laufzeittyp-Konvertierungsfunktion implementieren, die .Net TypeConverter verwendet, um die Konvertierung durchzuführen.Wie man einen .NET TypeConverter für einen bestimmten Typ sucht und aufruft?
Kann jemand einen TypConverter für einen bestimmten Typ nachschlagen und aufrufen?
Betrachten Sie diese C# Beispiel:
//
// 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 verwende 'dotnet' nicht für ein Tag. –