当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 扩展类型系统类型转换器

作者:精品下载站 日期:2024-12-14 20:29:21 浏览:15 分类:玩电脑

扩展类型系统类型转换器


当调用 LanguagePrimitives.ConvertTo(System.Object, System.Type) 方法时,ETS 使用两种基本类型的类型转换器。调用此方法时,PowerShell 尝试使用其标准 PowerShell 语言转换器或自定义转换器执行类型转换。如果 PowerShell 无法执行转换,则会引发 PSInvalidCastException 异常。

标准 Windows PowerShell 语言转换器

这些标准转换会在任何自定义转换之前进行检查,并且不能被覆盖。下表列出了调用 ConvertTo(System.Object, System.Type) 方法时 PowerShell 执行的类型转换。请注意,对 valueToConvertresultType 参数的引用是指 ConvertTo(System.Object,System.Type) 方法的参数。

From (valueToConvert) To (resultType) Returns Null String "" Null Char '\0' Null Numeric 0 of the type specified in the resultType parameter. Null Boolean Results of call to the IsTrue(System.Object)(Null) method. Null PSObject New object of type PSObject. Null Non-value-type Null. Null Nullable<T> Null. Derived Class Base class valueToConvert Anything Void AutomationNull.Value Anything String Calls ToString mechanism. Anything Boolean IsTrue(System.Object) (valueToConvert) Anything PSObject Results of call to the AsPSObject(System.Object) (valueToConvert) method. Anything Xml Document Converts valueToConvert to string, then calls XMLDocument constructor. Array Array Attempts to convert each element of the array. Singleton Array Array[0] equals valueToConvert that is converted to the element type of the array. IDictionary Hash table Results of call to Hashtable(valueToConvert). String Char[] valueToConvert.ToCharArray String RegEx Results of call to Regx(valueToConvert). String Type Returns the appropriate type using the valueToConvert parameter to search RunspaceConfiguration.Assemblies. String Numeric If valueToConvert is "", returns 0 of the resultType. Otherwise the culture "culture invariant" is used to produce a numeric value. Integer System.Enum Converts the integer to the constant if the integer is defined by the enumeration. If the integer is not defined an PSInvalidCastException exception is thrown.

自定义转换

如果 PowerShell 无法使用标准 PowerShell 语言转换器转换类型,则会检查自定义转换器。 PowerShell 按照本节中描述的顺序查找多种类型的自定义转换器。请注意,对 valueToConvertresultType 参数的引用是指 ConvertTo(System.Object, System.Type) 方法的参数。如果自定义转换器抛出异常,则不会进一步尝试转换该对象,并且该异常将被包装在随后抛出的 PSInvalidCastException 异常中。

PowerShell类型转换器

PowerShell 类型转换器用于转换单个类型或一系列类型,例如从 System.Enum 类派生的所有类型。要创建 PowerShell 类型转换器,您必须实现 PSTypeConverter 类并将该实现与目标类关联。有两种方法可以将 PowerShell 类型转换器与其目标类相关联。

  • 通过类型配置文件
  • 通过将 TypeConverterAttribute 属性应用于目标类

PowerShell 类型转换器派生自 PSTypeConverter 抽象类,提供将对象转换为特定类型或从特定类型转换的方法。如果 valueToConvert 参数包含具有与其关联的 PowerShell 类型转换器的对象,则 PowerShell 会调用 PSTypeConverter.ConvertTo(System.Object, System.Type,System.IFormatProvider, System.Boolean)关联转换器的 方法将对象转换为 resultType 参数指定的类型。如果 resultType 参数引用具有与其关联的 PowerShell 类型转换器的类型,则 PowerShell 会调用 PSTypeConverter.ConvertFrom(System.Object,System.Type, System.IFormatProvider, System.Boolean)关联转换器的 方法将对象从 resultType 参数指定的类型转换。

系统类型转换器

系统类型转换器用于转换特定的目标类。这种类型的转换器不能用于转换类族。要创建系统类型转换器,您必须实现 TypeConverter 类并将该实现与目标类关联。有两种方法可以将系统类型转换器与其目标类相关联。

  • 通过类型配置文件
  • 通过将 TypeConverterAttribute 属性应用于目标类

解析转换器

如果 valueToConvert 参数是字符串,并且 resultType 参数的对象类型具有 Parse 方法,则 Parse 调用 方法来转换字符串。

构造函数转换器

如果 resultType 参数的对象类型具有一个构造函数,该构造函数具有与 valueToConvert 参数的对象类型相同的单个参数,则调用此构造函数。

隐式转换运算符转换器

如果valueToConvert参数具有转换为resultType的隐式转换运算符,则调用其转换运算符。如果resultType参数具有从valueToConvert转换的隐式转换运算符,则调用其转换运算符。

显式强制转换运算符转换器

如果valueToConvert参数具有转换为resultType的显式转换运算符,则调用其转换运算符。如果 resultType 参数具有从 valueToConvert 进行转换的显式转换运算符,则会调用其转换运算符。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯