KGy SOFT

ObjectExtensionsConvert Method (Object, Type, CultureInfo)

KGy SOFT Core Libraries Help
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.

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

public static Object Convert(
	this Object obj,
	Type targetType,
	CultureInfo culture = null
)

Parameters

obj
Type: SystemObject
The object to convert.
targetType
Type: SystemType
The desired type of the return value.
culture (Optional)
Type: System.GlobalizationCultureInfo
The culture to use for the conversion. If , then the InvariantCulture will be used. This parameter is optional.
Default value: .

Return Value

Type: Object
An object of targetType, which is the result of the conversion.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Object. 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).
Exceptions

ExceptionCondition
ArgumentExceptionobj cannot be converted to targetType.
Remarks

New conversions can be registered by the RegisterConversion Type extension methods.

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.

targetType can be even a collection type if obj is also an IEnumerable implementation. The target collection type must have either a default constructor or a constructor that can accept a list, array or dictionary as an initializer collection.

See Also

Reference