TypeConverterAttribute 類別

定義

指定要用來做為此屬性所繫結至物件的型別轉換子。Specifies what type to use as a converter for the object this attribute is bound to.

public ref class TypeConverterAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class TypeConverterAttribute : Attribute
type TypeConverterAttribute = class
    inherit Attribute
Public NotInheritable Class TypeConverterAttribute
Inherits Attribute
繼承
TypeConverterAttribute
屬性

範例

下列範例MyClass會宣告為使用名MyClassConverter為的型別轉換子。The following example declares MyClass to use the type converter called MyClassConverter. 這個範例假設已MyClassConverter在其他位置執行。This example assumes that MyClassConverter has been implemented elsewhere. 執行轉換器的類別 (MyClassConverter) 必須繼承TypeConverter自類別。The class implementing the converter (MyClassConverter) must inherit from the TypeConverter class.

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

下一個範例會建立的實例MyClassThe next example creates an instance of MyClass. 然後, 它會取得類別的屬性, 並列印所使用MyClass之類型轉換器的名稱。Then it gets the attributes for the class, and prints the name of the type converter used by MyClass.

int main()
{
   // Creates a new instance of MyClass.
   Class1::MyClass^ myNewClass = gcnew Class1::MyClass;

   // Gets the attributes for the instance.
   AttributeCollection^ attributes = TypeDescriptor::GetAttributes( myNewClass );

   /* Prints the name of the type converter by retrieving the 
        * TypeConverterAttribute from the AttributeCollection. */
   TypeConverterAttribute^ myAttribute = dynamic_cast<TypeConverterAttribute^>(attributes[ TypeConverterAttribute::typeid ]);
   Console::WriteLine( "The type converter for this class is: {0}", myAttribute->ConverterTypeName );
   return 0;
}
public static int Main() {
    // Creates a new instance of MyClass.
    MyClass myNewClass = new MyClass();
 
    // Gets the attributes for the instance.
    AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewClass);
 
    /* Prints the name of the type converter by retrieving the 
     * TypeConverterAttribute from the AttributeCollection. */
    TypeConverterAttribute myAttribute = 
        (TypeConverterAttribute)attributes[typeof(TypeConverterAttribute)];
    
    Console.WriteLine("The type conveter for this class is: " + 
        myAttribute.ConverterTypeName);
 
    return 0;
 }
Public Shared Function Main() As Integer
    ' Creates a new instance of ClassA.
    Dim myNewClass As New ClassA()
    
    ' Gets the attributes for the instance.
    Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(myNewClass)
    
    ' Prints the name of the type converter by retrieving the
    ' TypeConverterAttribute from the AttributeCollection. 
    Dim myAttribute As TypeConverterAttribute = _
        CType(attributes(GetType(TypeConverterAttribute)), TypeConverterAttribute)
    
    Console.WriteLine(("The type conveter for this class is: " _
        + myAttribute.ConverterTypeName))
    Return 0
End Function 'Main

備註

您用於轉換的類別必須繼承自TypeConverterThe class you use for conversion must inherit from TypeConverter. ConverterTypeName使用屬性, 即可取得提供此屬性所系結物件之資料轉換的類別名稱。Use the ConverterTypeName property to get the name of the class that provides the data conversion for the object this attribute is bound to.

如需屬性的詳細資訊,請參閱屬性For more information about attributes, see Attributes. 如需類型轉換器的詳細資訊, 請TypeConverter參閱基底類別和如何:執行類型轉換器For more information about type converters, see the TypeConverter base class and How to: Implement a Type Converter.

為了在提供 XAML 類型轉換行為的自訂類別上建立類型轉換器, 您可以將TypeConverterAttribute屬性套用至您的類型。In order to establish a type converter on a custom class that provides type conversion behavior for XAML, you apply the TypeConverterAttribute attribute to your type. 屬性的引數會參考您的類型轉換器實作為。The argument of the attribute references your type converter implementation. 您的類型轉換器應該要能夠接受字串中的值, 以用於 XAML 標記中的屬性或初始化文字, 並將該字串轉換成您想要的目的地類型。Your type converter should be able to accept values from a string that is used for attributes or initialization text in XAML markup, and convert that string into your intended destination type. 如需詳細資訊, 請參閱TypeConverters 和 XAMLFor more information, see TypeConverters and XAML.

XAML 的類型轉換子行為也可以在特定屬性上建立, 而不是套用至類型的所有值。Rather than applying to all values of a type, a type converter behavior for XAML can also be established on a specific property. 在此情況下, 您TypeConverterAttribute會將套用至屬性定義 (外部定義, 而不getset特定的和定義)。In this case, you apply TypeConverterAttribute to the property definition (the outer definition, not the specific get and set definitions).

您可以藉由TypeConverterAttribute將套用get至支援 xaml 使用方式的方法存取子, 來指派自訂可附加成員的 XAML 使用類型轉換器行為。A type converter behavior for XAML usage of a custom attachable member can be assigned by applying TypeConverterAttribute to the get method accessor that supports the XAML usage. 如需詳細資訊,請參閱附加屬性概觀For more information, see Attached Properties Overview.

針對需要從物件執行時間取得其他狀態的複雜 XAML 序列化案例, 請考慮定義除了類型轉換器以外的值序列化程式, 而屬性同時支援自訂類型或自訂成員上的類別。For complex XAML serialization cases that require additional state from the object runtime, consider defining a value serializer in addition to a type converter, and attribute both support classes on your custom types or custom members. 如需詳細資訊,請參閱ValueSerializerFor more information, see ValueSerializer.

建構函式

TypeConverterAttribute()

使用預設型別轉換子,也就是空字串 (""),來初始化 TypeConverterAttribute 類別的新執行個體。Initializes a new instance of the TypeConverterAttribute class with the default type converter, which is an empty string ("").

TypeConverterAttribute(String)

使用指定型別名稱做為這個屬性所繫結之物件的資料轉換子,來初始化 TypeConverterAttribute 類別的新執行個體。Initializes a new instance of the TypeConverterAttribute class, using the specified type name as the data converter for the object this attribute is bound to.

TypeConverterAttribute(Type)

使用指定型別做為這個屬性所繫結之物件的資料轉換子,來初始化 TypeConverterAttribute 類別的新執行個體。Initializes a new instance of the TypeConverterAttribute class, using the specified type as the data converter for the object this attribute is bound to.

欄位

Default

指定這個屬性繫結物件所要用來做為轉換子的型別。Specifies the type to use as a converter for the object this attribute is bound to.

屬性

ConverterTypeName

取得 Type 的完整型別名稱來做為這個屬性所繫結之物件的轉換子。Gets the fully qualified type name of the Type to use as a converter for the object this attribute is bound to.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,表示指定的物件值是否等於目前的 TypeConverterAttributeReturns whether the value of the given object is equal to the current TypeConverterAttribute.

GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

GetType()

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

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

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

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱