KGy SOFT

TypeExtensionsRegisterConversion Method (Type, Type, ConversionAttempt)

KGy SOFT Core Libraries Help
Registers a ConversionAttempt from the specified sourceType to targetType.

Namespace:  KGySoft.CoreLibraries
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0-rc.1
Syntax

public static void RegisterConversion(
	this Type sourceType,
	Type targetType,
	ConversionAttempt conversion
)

Parameters

sourceType
Type: SystemType
The source Type for which the conversion can be called.
targetType
Type: SystemType
The result Type that conversion produces.
conversion
Type: KGySoft.CoreLibrariesConversionAttempt
A ConversionAttempt delegate, which is able to perform the conversion.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Type. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks

After calling this method the Convert/TryConvert Object extension methods and Parse/TryParse String extension methods will be able to use the registered conversion between sourceType and targetType.

Calling the RegisterConversion methods for the same source and target types multiple times will override the old registered conversion with the new one.

Tip Tip
The registered conversions are tried to be used for intermediate conversion steps if possible. For example, if a conversion is registered from Int64 to IntPtr, then conversions from other convertible types become automatically available using the Int64 type as an intermediate conversion step.

sourceType and targetType can be interface, abstract or even a generic type definition. Preregistered conversions:

  • KeyValuePairTKey, TValue to another KeyValuePairTKey, TValue
  • KeyValuePairTKey, TValue to DictionaryEntry
  • DictionaryEntry to KeyValuePairTKey, TValue

See Also

Reference