Partager via


LanguagePrimitives.ConvertTo Method

Definition

Overloads

ConvertTo(Object, Type)

Converts valueToConvert to resultType.

ConvertTo(Object, Type, IFormatProvider)

Converts valueToConvert to resultType possibly considering formatProvider.

ConvertTo<T>(Object)

Generic convertto that simplifies working with workflow.

ConvertTo(Object, Type)

Converts valueToConvert to resultType.

public:
 static System::Object ^ ConvertTo(System::Object ^ valueToConvert, Type ^ resultType);
public:
 static Platform::Object ^ ConvertTo(Platform::Object ^ valueToConvert, Platform::Type ^ resultType);
public static object ConvertTo (object valueToConvert, Type resultType);
static member ConvertTo : obj * Type -> obj
Public Shared Function ConvertTo (valueToConvert As Object, resultType As Type) As Object

Parameters

valueToConvert
Object

Value to be converted and returned.

resultType
Type

Type to convert valueToConvert.

Returns

Converted value.

Exceptions

If resultType is null.

If the conversion failed.

Remarks

A null valueToConvert can be converted to : string - returns "" char - returns '\0' numeric types - returns 0 converted to the appropriate type boolean - returns LanguagePrimitives.IsTrue(null) PSObject - returns new PSObject()) array - returns an array with null in array[0] non value types - returns null

The following conversions are considered language standard and cannot be customized: - from derived to base class - returns valueToConvert intact - to PSObject - returns PSObject.AsPSObject(valueToConvert) - to void - returns AutomationNull.Value - from array/IEnumerable to array - tries to convert array/IEnumerable elements - from object of type X to array of X - returns an array with object as its only element - to bool - returns LanguagePrimitives.IsTrue(valueToConvert) - to string - returns a string representation of the object. In the particular case of a number to string, the conversion is culture invariant. - from IDictionary to Hashtable - uses the Hashtable constructor - to XmlDocument - creates a new XmlDocument with the string representation of valueToConvert - from string to char[] - returns ((string)valueToConvert).ToCharArray() - from string to RegEx - creates a new RegEx with the string - from string to Type - looks up the type in the minishell's assemblies - from empty string to numeric - returns 0 converted to the appropriate type - from string to numeric - returns a culture invariant conversion - from ScriptBlock to Delegate - returns a delegate wrapping that scriptblock. - from Integer to Enumeration - Uses Enum.ToObject - to WMI - Instantiate a WMI instance using System.Management.ManagementObject - to WMISearcher - returns objects from running WQL query with the string representation of valueToConvert. The query is run using ManagementObjectSearcher Class. - to WMIClass - returns ManagementClass represented by the string representation of valueToConvert. - to ADSI - returns DirectoryEntry represented by the string representation of valueToConvert. - to ADSISearcher - return DirectorySearcher represented by the string representation of valueToConvert.

If none of the cases above is true, the following is considered in order:

1) TypeConverter and PSTypeConverter 2) the Parse methods if the valueToConvert is a string 3) Constructors in resultType that take one parameter with type valueToConvert.GetType() 4) Implicit and explicit cast operators 5) IConvertible

If any operation above throws an exception, this exception will be wrapped into a PSInvalidCastException and thrown resulting in no further conversion attempt.

Applies to

ConvertTo(Object, Type, IFormatProvider)

Converts valueToConvert to resultType possibly considering formatProvider.

public:
 static System::Object ^ ConvertTo(System::Object ^ valueToConvert, Type ^ resultType, IFormatProvider ^ formatProvider);
public static object ConvertTo (object valueToConvert, Type resultType, IFormatProvider formatProvider);
static member ConvertTo : obj * Type * IFormatProvider -> obj
Public Shared Function ConvertTo (valueToConvert As Object, resultType As Type, formatProvider As IFormatProvider) As Object

Parameters

valueToConvert
Object

Value to be converted and returned.

resultType
Type

Type to convert valueToConvert.

formatProvider
IFormatProvider

To be used in custom type conversions, to call parse and to call Convert.ChangeType.

Returns

Converted value.

Exceptions

If resultType is null.

If the conversion failed.

Remarks

A null valueToConvert can be converted to : string - returns "" char - returns '\0' numeric types - returns 0 converted to the appropriate type boolean - returns LanguagePrimitives.IsTrue(null) PSObject - returns new PSObject()) array - returns an array with null in array[0] non value types - returns null

The following conversions are considered language standard and cannot be customized: - from derived to base class - returns valueToConvert intact - to PSObject - returns PSObject.AsPSObject(valueToConvert) - to void - returns AutomationNull.Value - from array/IEnumerable to array - tries to convert array/IEnumerable elements - from object of type X to array of X - returns an array with object as its only element - to bool - returns LanguagePrimitives.IsTrue(valueToConvert) - to string - returns a string representation of the object. In the particular case of a number to string, the conversion is culture invariant. - from IDictionary to Hashtable - uses the Hashtable constructor - to XmlDocument - creates a new XmlDocument with the string representation of valueToConvert - from string to char[] - returns ((string)valueToConvert).ToCharArray() - from string to RegEx - creates a new RegEx with the string - from string to Type - looks up the type in the minishell's assemblies - from empty string to numeric - returns 0 converted to the appropriate type - from string to numeric - returns a culture invariant conversion - from ScriptBlock to Delegate - returns a delegate wrapping that scriptblock. - from Integer to Enumeration - Uses Enum.ToObject

If none of the cases above is true, the following is considered in order:

1) TypeConverter and PSTypeConverter 2) the Parse methods if the valueToConvert is a string 3) Constructors in resultType that take one parameter with type valueToConvert.GetType() 4) Implicit and explicit cast operators 5) IConvertible

If any operation above throws an exception, this exception will be wrapped into a PSInvalidCastException and thrown resulting in no further conversion attempt.

Applies to

ConvertTo<T>(Object)

Generic convertto that simplifies working with workflow.

public:
generic <typename T>
 static T ConvertTo(System::Object ^ valueToConvert);
public:
generic <typename T>
 static T ConvertTo(Platform::Object ^ valueToConvert);
template <typename T>
 static T ConvertTo(winrt::Windows::Foundation::IInspectable const & valueToConvert);
public static T ConvertTo<T> (object valueToConvert);
static member ConvertTo : obj -> 'T
Public Shared Function ConvertTo(Of T) (valueToConvert As Object) As T

Type Parameters

T

The type of object to return

Parameters

valueToConvert
Object

Returns

T

Applies to