TypeConverter 類別

定義

會提供轉換實值型別為其他型別,以及存取標準值和子屬性的統一方式。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
繼承
TypeConverter
衍生
屬性

範例

下列程式碼範例示範如何建立類型轉換器的實例, 並將它系結至類別。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

當您具有具有列舉的屬性時, 請先查看列舉值是否有效, 再設定屬性。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儲存在變數c中的名稱。The 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. 類別的類型轉換子會系結至具有TypeConverterAttribute的類別。The 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. 相反地, 請使用TypeDescriptor來存取適當的轉換器。Instead, access the appropriate converter by using TypeDescriptor. 如需詳細資訊, 請參閱所提供的程式碼範例。For more information, see the code examples provided.

不過, 使用 xaml 時, xaml 處理器會TypeConverterAttribute直接搜尋, 而不TypeDescriptor會進行。However, 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) 用途類型轉換器的詳細資訊, 請參閱[如何:執行類型轉換器](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/ayybcxe5(v=vs.120))或[一般化類型轉換](https://msdn.microsoft.com/library/49253ae6-7657-4810-82ab-1176a6feeada)。For more information about type converters for general (non-XAML) purposes, see [How to: Implement a Type Converter](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2013/ayybcxe5(v=vs.120)) or [Generalized Type Conversion](https://msdn.microsoft.com/library/49253ae6-7657-4810-82ab-1176a6feeada).

建構函式

TypeConverter()

初始化 TypeConverter 類別的新執行個體。Initializes a new instance of the TypeConverter class.

方法

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)

傳回值,指出這個轉換子是否可將指定之型別的物件轉換為這個轉換子的型別。Returns whether this converter can convert an object of the given type to the type of this converter.

CanConvertTo(ITypeDescriptorContext, Type)

傳回這個轉換器是否可以使用指定的內容,將物件轉換成指定的類型。Returns whether this converter can convert the object to the specified type, using the specified context.

CanConvertTo(Type)

傳回值,指出這個轉換子是否可以將物件轉換成指定的型別。Returns whether this converter can convert the object to the specified type.

ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

使用指定的內容和文化特性資訊,將指定物件轉換為這個轉換子的類型。Converts the given object to the type of this converter, using the specified context and culture information.

ConvertFrom(Object)

會將指定值轉換為這個轉換子的型別。Converts the given value to the type of this converter.

ConvertFromInvariantString(ITypeDescriptorContext, String)

會使用不區分的文化特性和指定的內容,將指定的字串轉換為這個轉換子的型別。Converts the given string to the type of this converter, using the invariant culture and the specified context.

ConvertFromInvariantString(String)

會使用不因文化特性而異,將指定字串轉換為這個轉換子的型別。Converts the given string to the type of this converter, using the invariant culture.

ConvertFromString(ITypeDescriptorContext, CultureInfo, String)

會使用指定內容和文化特性資訊,將指定文字轉換為物件。Converts the given text to an object, using the specified context and culture information.

ConvertFromString(ITypeDescriptorContext, String)

會使用指定的內容,將指定文字轉換為物件。Converts the given text to an object, using the specified context.

ConvertFromString(String)

會將指定的文字轉換成物件。Converts the specified text to an object.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

使用指定的內容和文化特性資訊,將指定值物件轉換成指定類型。Converts the given value object to the specified type, using the specified context and culture information.

ConvertTo(Object, Type)

會使用引數,將指定值物件轉換成指定型別。Converts the given value object to the specified type, using the arguments.

ConvertToInvariantString(ITypeDescriptorContext, Object)

會使用指定的內容,將指定值轉換成不因文化特性而異的字串表示。Converts the specified value to a culture-invariant string representation, using the specified context.

ConvertToInvariantString(Object)

會將指定值轉換成不因文化特性而異的字串表示。Converts the specified value to a culture-invariant string representation.

ConvertToString(ITypeDescriptorContext, CultureInfo, Object)

會使用指定的內容和文化特性資訊,將指定值轉換為字串表示。Converts the given value to a string representation, using the specified context and culture information.

ConvertToString(ITypeDescriptorContext, Object)

會使用指定的內容,將指定值轉換為字串表示。Converts the given value to a string representation, using the given context.

ConvertToString(Object)

會將指定值轉換成字串表示。Converts the specified value to a string representation.

CreateInstance(IDictionary)

重新建立 Object,需為物件提供一組屬性值。Re-creates an Object given a set of property values for the object.

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)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetConvertFromException(Object)

無法執行轉換時,會傳回要擲回的例外狀況。Returns an exception to throw when a conversion cannot be performed.

GetConvertToException(Object, Type)

無法執行轉換時,會傳回要擲回的例外狀況。Returns an exception to throw when a conversion cannot be performed.

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)

會使用指定的內容,傳回在這個物件上變更值是否需要呼叫 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()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 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[])

使用指定的內容和屬性 (Attribute),傳回由值參數所指定之陣列型別的屬性 (Property) 集合。Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.

GetProperties(Object)

傳回由值參數所指定之陣列型別的屬性集合。Returns a collection of properties for the type of array specified by the value parameter.

GetPropertiesSupported()

傳回值,指出這個物件是否支援屬性。Returns whether this object supports properties.

GetPropertiesSupported(ITypeDescriptorContext)

會使用指定的內容傳回數值,表示這個物件是否支援屬性。Returns whether this object supports properties, using the specified context.

GetStandardValues()

從為這個型別轉換子所設計的資料型別預設內容中,會傳回標準值的集合。Returns a collection of standard values from the default context for the data type this type converter is designed for.

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

傳回值,這個值表示從 GetStandardValues() 所傳回的標準值集合是否為獨佔清單。Returns whether the collection of standard values returned from GetStandardValues() is an exclusive list.

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

會傳回這個物件是否支援可以從清單中挑選的標準值組。Returns whether this object supports a standard set of values that can be picked from a list.

GetStandardValuesSupported(ITypeDescriptorContext)

會使用指定的內容傳回,這個物件是否支援可從清單中挑選的標準值集合。Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsValid(ITypeDescriptorContext, Object)

會傳回指定值物件是否對這個型別和指定的內容有效。Returns whether the given value object is valid for this type and for the specified context.

IsValid(Object)

會傳回指定值物件是否對這個型別有效。Returns whether the given value object is valid for this type.

MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
SortProperties(PropertyDescriptorCollection, String[])

排序屬性的集合。Sorts a collection of properties.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱