TypeConverter TypeConverter TypeConverter TypeConverter Class

定义

提供一种将值的类型转换为其他类型以及访问标准值和子属性的统一方法。Provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.

public ref class TypeConverter
[System.Runtime.InteropServices.ComVisible(true)]
public class TypeConverter
type TypeConverter = class
Public Class TypeConverter
继承
TypeConverterTypeConverterTypeConverterTypeConverter
派生
属性

示例

下面的代码示例演示如何创建类型转换器的实例,并将其绑定到一个类。The following code example shows how to create an instance of a type converter and bind it to a class. 实现转换器的类MyClassConverter,必须继承自TypeConverter类。The class implementing the converter, MyClassConverter, must inherit from the TypeConverter class.

public:
   [TypeConverter(Sample::MyClassConverter::typeid)]
   ref class MyClass
   {
      // Insert code here.
   };
[TypeConverter(typeof(MyClassConverter))]
 public class MyClass {
    // Insert code here.
}
<TypeConverter(GetType(MyClassConverter))> _
Public Class Class1
    ' Insert code here.
End Class 'MyClass

在必须有一个枚举的属性,请检查枚举值设置属性前是否有效。When you have a property that has an enumeration, check to see whether an enumeration value is valid before setting the property. 下一步的代码示例需要一个枚举,称为MyPropertyEnum已声明。The next code example requires that an enumeration called MyPropertyEnum has been declared.

public:
   property MyPropertyEnum MyProperty 
   {
      void set( MyPropertyEnum value )
      {
         // Checks to see if the value passed is valid.
         if ( !TypeDescriptor::GetConverter( MyPropertyEnum::typeid )->IsValid( value ) )
         {
            throw gcnew ArgumentException;
         }
         // The value is valid. Insert code to set the property.
      }
   }
public MyPropertyEnum MyProperty {
    set {
       // Checks to see if the value passed is valid.
       if (!TypeDescriptor.GetConverter(typeof(MyPropertyEnum)).IsValid(value)) {
          throw new ArgumentException();
       }
       // The value is valid. Insert code to set the property.
    }
 }

Public WriteOnly Property MyProperty() As MyPropertyEnum
    Set
        ' Checks to see if the value passed is valid.
        If Not TypeDescriptor.GetConverter(GetType(MyPropertyEnum)).IsValid(value) Then
            Throw New ArgumentException()
        End If
        ' The value is valid. Insert code to set the property.
    End Set 
End Property

另一个常见的类型转换器用法是将对象转换为字符串。Another common type converter usage is to convert an object to a string. 下面的代码示例打印出的名称Color变量中存储cThe following code example prints out the name of the Color stored in the variable c.

Color c = Color::Red;
Console::WriteLine( TypeDescriptor::GetConverter( c )->ConvertToString( c ) );
Color c = Color.Red;
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
Dim c As Color = Color.Red
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c))

此外可以使用类型转换器将值转换的名称,如下面的代码示例中所示。You can also use a type converter to convert a value from its name, as shown in the next code example.

Color c =  (Color)(TypeDescriptor::GetConverter( Color::typeid )->ConvertFromString( "Red" ));
Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
Dim c As Color = CType(TypeDescriptor.GetConverter(GetType(Color)).ConvertFromString("Red"), Color)

在下面的代码示例中,可以使用类型转换器来打印出的对象支持的标准值集。In the following code example, you can use a type converter to print out the set of standard values that the object supports.

for each ( Color c in TypeDescriptor::GetConverter( Color::typeid )->GetStandardValues() )
{
   Console::WriteLine( TypeDescriptor::GetConverter( c )->ConvertToString( c ) );
}
foreach(Color c in TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues()) {
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
 }
Dim c As Color
For Each c In  TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues()
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c))
Next c

注解

最常见类型是转换器的指将转换为和从文本表示形式。The most common type of converter is one that converts to and from a text representation. 一个类的类型转换器绑定到与类TypeConverterAttributeThe type converter for a class is bound to the class with a TypeConverterAttribute. 除非此特性重写,否则所有从此类继承的类作为基类使用相同的类型转换器。Unless this attribute is overridden, all classes that inherit from this class use the same type converter as the base class.

备注

用于常规类型系统开销不类型转换器直接访问。For general type system purposes, do not access a type converter directly. 相反,使用访问相应的转换器TypeDescriptorInstead, access the appropriate converter by using TypeDescriptor. 有关详细信息,请参阅提供的代码示例。For more information, see the code examples provided.

但是,使用 XAML,XAML 处理器会搜索TypeConverterAttribute直接,而无需通过TypeDescriptorHowever, when using XAML, a XAML processor searches for the TypeConverterAttribute directly, instead of going through TypeDescriptor. 在你希望的情况下TypeDescriptor实例在代码中,或在 WPF 中创建的共享的实例的资源,是可接受的构造不直接引用TypeDescriptor或支持其他反射和类型系统。For cases where you do want a TypeDescriptor instance from code, or where you create a shared instance in WPF resources, it is acceptable to construct it directly without referencing TypeDescriptor or other reflection and type system support.

类派生自TypeConverter经常被引用的 XAML 处理器如何将属性或初始化文本值转换从标记 (这是本质上是一个字符串) 的一部分,并生成一个对象,用于运行时表示形式。Classes derived from TypeConverter are often referenced as part of how a XAML processor converts an attribute or initialization text value from markup (which is inherently a string) and generates an object for a run-time representation. 打算通常为 XAML 支持类型转换行为的自定义类型作者实现TypeConverter支持其自己唯一的类ConvertFrom从字符串的行为。Custom type authors that intend to support a type conversion behavior for XAML typically implement a TypeConverter class that supports their own unique ConvertFrom behavior from a string. 此行为允许从作为 XAML 属性值提供的字符串类型转换,并提供 XAML 处理器需要执行从字符串中,创建一个对象,以便可以在一个已分析的对象图中生成对象的支持。This behavior enables type conversion from the string provided as a XAML attribute value and provides a XAML processor with the support needed to create an object from the string, so that the object can be produced in a parsed object graph. 自定义类型或自定义类型的成员所指示的应用TypeConverterAttribute的定义,且该特性引用自定义TypeConverter实现。Custom types or members of custom types are indicated by applying TypeConverterAttribute to the definitions, with the attribute referencing the custom TypeConverter implementation. 有关详细信息,请参阅XAML 概述的类型转换器For more information, see Type Converters for XAML Overview.

继承者说明

继承自TypeConverter来实现您自己的转换要求。Inherit from TypeConverter to implement your own conversion requirements. 当从此类继承时,可以重写以下方法:When you inherit from this class, you can override the following methods: -若要支持自定义类型转换,重写CanConvertFrom(ITypeDescriptorContext, Type)CanConvertTo(ITypeDescriptorContext, Type)ConvertFrom(ITypeDescriptorContext, CultureInfo, Object),和ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)方法。- To support custom type conversion, override the CanConvertFrom(ITypeDescriptorContext, Type), CanConvertTo(ITypeDescriptorContext, Type), ConvertFrom(ITypeDescriptorContext, CultureInfo, Object), and ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) methods.

-若要转换的类型,必须重新创建要将其值更改的对象,重写CreateInstance(ITypeDescriptorContext, IDictionary)GetCreateInstanceSupported(ITypeDescriptorContext)方法。- To convert types that must re-create the object to change its value, override the CreateInstance(ITypeDescriptorContext, IDictionary) and GetCreateInstanceSupported(ITypeDescriptorContext) methods.

-若要将转换支持属性的类型,请重写GetProperties(ITypeDescriptorContext, Object, Attribute[])GetPropertiesSupported(ITypeDescriptorContext)方法。- To convert types that support properties, override the GetProperties(ITypeDescriptorContext, Object, Attribute[]) and GetPropertiesSupported(ITypeDescriptorContext) methods. 如果要转换的类不具有属性,并且您需要实现属性,则可以使用TypeConverter.SimplePropertyDescriptor类作为实现的属性说明符的基础。If the class you are converting does not have properties, and you need to implement properties, you can use the TypeConverter.SimplePropertyDescriptor class as a base for implementing the property descriptors. 继承自TypeConverter.SimplePropertyDescriptor,必须重写GetValue(Object)SetValue(Object, Object)方法。When you inherit from TypeConverter.SimplePropertyDescriptor, you must override the GetValue(Object) and SetValue(Object, Object) methods.

-若要将转换支持标准值的类型,请重写GetStandardValues(ITypeDescriptorContext)GetStandardValuesExclusive(ITypeDescriptorContext)GetStandardValuesSupported(ITypeDescriptorContext)IsValid(ITypeDescriptorContext, Object)方法。- To convert types that support standard values, override the GetStandardValues(ITypeDescriptorContext), GetStandardValuesExclusive(ITypeDescriptorContext), GetStandardValuesSupported(ITypeDescriptorContext) and IsValid(ITypeDescriptorContext, Object) methods.

您的派生的类型可能被标记为internalprivate,但在类型的实例可以使用创建TypeDescriptor类。Your derived type might be marked as internal or private, but an instance of your type can be created with the TypeDescriptor class. 不要编写不安全的代码假定调用方是受信任。Do not write insecure code by assuming the caller is trusted. 而是假设调用方可能会在部分信任环境中创建类型的实例。Assume instead that callers might create instances of your type in partial trust.

有关常规 (非 XAML) 用于类型转换器的详细信息,请参阅如何:实现类型转换器通用化类型转换For more information about type converters for general (non-XAML) purposes, see How to: Implement a Type Converter or Generalized Type Conversion.

构造函数

TypeConverter() TypeConverter() TypeConverter() TypeConverter()

初始化 TypeConverter 类的新实例。Initializes a new instance of the TypeConverter class.

方法

CanConvertFrom(ITypeDescriptorContext, Type) CanConvertFrom(ITypeDescriptorContext, Type) CanConvertFrom(ITypeDescriptorContext, Type) CanConvertFrom(ITypeDescriptorContext, Type)

返回该转换器是否可以使用指定上下文将给定类型的对象转换为此转换器的类型。Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.

CanConvertFrom(Type) CanConvertFrom(Type) CanConvertFrom(Type) CanConvertFrom(Type)

返回该转换器是否可以将给定类型的对象转换为此转换器的类型。Returns whether this converter can convert an object of the given type to the type of this converter.

CanConvertTo(ITypeDescriptorContext, Type) CanConvertTo(ITypeDescriptorContext, Type) CanConvertTo(ITypeDescriptorContext, Type) CanConvertTo(ITypeDescriptorContext, Type)

返回此转换器能否使用指定上下文将对象转换为指定类型。Returns whether this converter can convert the object to the specified type, using the specified context.

CanConvertTo(Type) CanConvertTo(Type) CanConvertTo(Type) CanConvertTo(Type)

返回此转换器是否可将该对象转换为指定的类型。Returns whether this converter can convert the object to the specified type.

ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

使用指定的上下文和区域性信息将给定对象转换为此转换器的类型。Converts the given object to the type of this converter, using the specified context and culture information.

ConvertFrom(Object) ConvertFrom(Object) ConvertFrom(Object) ConvertFrom(Object)

将给定值转换为此转换器的类型。Converts the given value to the type of this converter.

ConvertFromInvariantString(ITypeDescriptorContext, String) ConvertFromInvariantString(ITypeDescriptorContext, String) ConvertFromInvariantString(ITypeDescriptorContext, String) ConvertFromInvariantString(ITypeDescriptorContext, String)

使用不变区域性和指定上下文将给定字符串转换为此转换器的类型。Converts the given string to the type of this converter, using the invariant culture and the specified context.

ConvertFromInvariantString(String) ConvertFromInvariantString(String) ConvertFromInvariantString(String) ConvertFromInvariantString(String)

使用不变区域性将给定字符串转换为此转换器的类型。Converts the given string to the type of this converter, using the invariant culture.

ConvertFromString(ITypeDescriptorContext, CultureInfo, String) ConvertFromString(ITypeDescriptorContext, CultureInfo, String) ConvertFromString(ITypeDescriptorContext, CultureInfo, String) ConvertFromString(ITypeDescriptorContext, CultureInfo, String)

使用指定的上下文和区域性信息将给定的文本转换为对象。Converts the given text to an object, using the specified context and culture information.

ConvertFromString(ITypeDescriptorContext, String) ConvertFromString(ITypeDescriptorContext, String) ConvertFromString(ITypeDescriptorContext, String) ConvertFromString(ITypeDescriptorContext, String)

使用指定的上下文将给定的文本转换为对象。Converts the given text to an object, using the specified context.

ConvertFromString(String) ConvertFromString(String) ConvertFromString(String) ConvertFromString(String)

将指定文本转换为对象。Converts the specified text to an object.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

使用指定的上下文和区域性信息将给定值对象转换为指定的类型。Converts the given value object to the specified type, using the specified context and culture information.

ConvertTo(Object, Type) ConvertTo(Object, Type) ConvertTo(Object, Type) ConvertTo(Object, Type)

使用参数将给定的值对象转换为指定的类型。Converts the given value object to the specified type, using the arguments.

ConvertToInvariantString(ITypeDescriptorContext, Object) ConvertToInvariantString(ITypeDescriptorContext, Object) ConvertToInvariantString(ITypeDescriptorContext, Object) ConvertToInvariantString(ITypeDescriptorContext, Object)

使用指定的上下文将指定的值转换为区域性不变的字符串表示形式。Converts the specified value to a culture-invariant string representation, using the specified context.

ConvertToInvariantString(Object) ConvertToInvariantString(Object) ConvertToInvariantString(Object) ConvertToInvariantString(Object)

将指定值转换为区域性不变的字符串表示形式。Converts the specified value to a culture-invariant string representation.

ConvertToString(ITypeDescriptorContext, CultureInfo, Object) ConvertToString(ITypeDescriptorContext, CultureInfo, Object) ConvertToString(ITypeDescriptorContext, CultureInfo, Object) ConvertToString(ITypeDescriptorContext, CultureInfo, Object)

使用指定的上下文和区域性信息将给定的值转换为字符串表示形式。Converts the given value to a string representation, using the specified context and culture information.

ConvertToString(ITypeDescriptorContext, Object) ConvertToString(ITypeDescriptorContext, Object) ConvertToString(ITypeDescriptorContext, Object) ConvertToString(ITypeDescriptorContext, Object)

使用给定的上下文将给定的值转换为字符串表示形式。Converts the given value to a string representation, using the given context.

ConvertToString(Object) ConvertToString(Object) ConvertToString(Object) ConvertToString(Object)

将指定值转换为字符串表示形式。Converts the specified value to a string representation.

CreateInstance(IDictionary) CreateInstance(IDictionary) CreateInstance(IDictionary) CreateInstance(IDictionary)

在给定 Object 的一组属性值的情况下重新创建该对象。Re-creates an Object given a set of property values for the object.

CreateInstance(ITypeDescriptorContext, IDictionary) CreateInstance(ITypeDescriptorContext, IDictionary) CreateInstance(ITypeDescriptorContext, IDictionary) CreateInstance(ITypeDescriptorContext, IDictionary)

给定该对象的一组属性值,使用指定上下文创建与此 TypeConverter 相关联的类型的实例。Creates an instance of the type that this TypeConverter is associated with, using the specified context, given a set of property values for the object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetConvertFromException(Object) GetConvertFromException(Object) GetConvertFromException(Object) GetConvertFromException(Object)

返回无法执行转换时引发的异常。Returns an exception to throw when a conversion cannot be performed.

GetConvertToException(Object, Type) GetConvertToException(Object, Type) GetConvertToException(Object, Type) GetConvertToException(Object, Type)

返回无法执行转换时引发的异常。Returns an exception to throw when a conversion cannot be performed.

GetCreateInstanceSupported() GetCreateInstanceSupported() GetCreateInstanceSupported() GetCreateInstanceSupported()

返回更改此对象的值是否求调用 CreateInstance(IDictionary) 方法来创建新值。Returns whether changing a value on this object requires a call to the CreateInstance(IDictionary) method to create a new value.

GetCreateInstanceSupported(ITypeDescriptorContext) GetCreateInstanceSupported(ITypeDescriptorContext) GetCreateInstanceSupported(ITypeDescriptorContext) GetCreateInstanceSupported(ITypeDescriptorContext)

返回有关更改该对象上的某个值是否需要使用指定的上下文调用 CreateInstance(IDictionary) 以创建新值的情况。Returns whether changing a value on this object requires a call to CreateInstance(IDictionary) to create a new value, using the specified context.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetProperties(ITypeDescriptorContext, Object) GetProperties(ITypeDescriptorContext, Object) GetProperties(ITypeDescriptorContext, Object) GetProperties(ITypeDescriptorContext, Object)

使用指定的上下文返回值参数指定的数组类型的属性的集合。Returns a collection of properties for the type of array specified by the value parameter, using the specified context.

GetProperties(ITypeDescriptorContext, Object, Attribute[]) GetProperties(ITypeDescriptorContext, Object, Attribute[]) GetProperties(ITypeDescriptorContext, Object, Attribute[]) GetProperties(ITypeDescriptorContext, Object, Attribute[])

使用指定的上下文和特性返回由 value 参数指定的数组类型的属性的集合。Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.

GetProperties(Object) GetProperties(Object) GetProperties(Object) GetProperties(Object)

返回由值参数指定的数组类型的属性集合。Returns a collection of properties for the type of array specified by the value parameter.

GetPropertiesSupported() GetPropertiesSupported() GetPropertiesSupported() GetPropertiesSupported()

返回此对象是否支持属性。Returns whether this object supports properties.

GetPropertiesSupported(ITypeDescriptorContext) GetPropertiesSupported(ITypeDescriptorContext) GetPropertiesSupported(ITypeDescriptorContext) GetPropertiesSupported(ITypeDescriptorContext)

使用指定的上下文返回该对象是否支持属性。Returns whether this object supports properties, using the specified context.

GetStandardValues() GetStandardValues() GetStandardValues() GetStandardValues()

从默认的上下文返回此类型转换器设计用于的数据类型的标准值集合。Returns a collection of standard values from the default context for the data type this type converter is designed for.

GetStandardValues(ITypeDescriptorContext) GetStandardValues(ITypeDescriptorContext) GetStandardValues(ITypeDescriptorContext) GetStandardValues(ITypeDescriptorContext)

当与格式上下文一起提供时,返回此类型转换器设计用于的数据类型的标准值集合。Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.

GetStandardValuesExclusive() GetStandardValuesExclusive() GetStandardValuesExclusive() GetStandardValuesExclusive()

返回从 GetStandardValues() 返回的标准值的集合是否为独占列表。Returns whether the collection of standard values returned from GetStandardValues() is an exclusive list.

GetStandardValuesExclusive(ITypeDescriptorContext) GetStandardValuesExclusive(ITypeDescriptorContext) GetStandardValuesExclusive(ITypeDescriptorContext) GetStandardValuesExclusive(ITypeDescriptorContext)

使用指定的上下文返回从 GetStandardValues() 返回的标准值的集合是否为可能值的独占列表。Returns whether the collection of standard values returned from GetStandardValues() is an exclusive list of possible values, using the specified context.

GetStandardValuesSupported() GetStandardValuesSupported() GetStandardValuesSupported() GetStandardValuesSupported()

返回此对象是否支持可以从列表中选取的标准值集。Returns whether this object supports a standard set of values that can be picked from a list.

GetStandardValuesSupported(ITypeDescriptorContext) GetStandardValuesSupported(ITypeDescriptorContext) GetStandardValuesSupported(ITypeDescriptorContext) GetStandardValuesSupported(ITypeDescriptorContext)

使用指定的上下文返回此对象是否支持可以从列表中选取的标准值集。Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsValid(ITypeDescriptorContext, Object) IsValid(ITypeDescriptorContext, Object) IsValid(ITypeDescriptorContext, Object) IsValid(ITypeDescriptorContext, Object)

返回给定值对象对于此类型和此指定的上下文是否有效。Returns whether the given value object is valid for this type and for the specified context.

IsValid(Object) IsValid(Object) IsValid(Object) IsValid(Object)

返回给定值对象对于此类型是否有效。Returns whether the given value object is valid for this type.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
SortProperties(PropertyDescriptorCollection, String[]) SortProperties(PropertyDescriptorCollection, String[]) SortProperties(PropertyDescriptorCollection, String[]) SortProperties(PropertyDescriptorCollection, String[])

对属性的集合进行排序。Sorts a collection of properties.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅