TypeExtensionsRegisterConversion(Type, Type, Conversion) Method

Registers a Conversion from the specified sourceType to targetType.


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


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).


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.

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


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.

The UnregisterConversion methods can be used to remove a registered conversion. Registered conversions can be removed in any order but always the lastly set will be the active one.


If you want to permanently set a custom conversion, then it is recommended to register it at the start of your application or service. It is also possible to register a conversion temporarily, and then restore the previous one by calling the UnregisterConversion methods but then you must make sure there is no running concurrent operation on a different thread that suppose to use different conversion for the same type.

See Also