TypeExtensionsRegisterConversion(Type, Type, Conversion) Method

Registers a Conversion from the specified sourceType to targetType.

Definition

Namespace: KGySoft.CoreLibraries
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 7.2.0
C#
public static void RegisterConversion(
	this Type sourceType,
	Type targetType,
	Conversion conversion
)

Parameters

sourceType  Type
The source Type for which the conversion can be called.
targetType  Type
The result Type that conversion produces.
conversion  Conversion
A Conversion 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

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:

See Also