共用方式為


TypeConverterAttribute 類別

定義

指定要用來做為此屬性所繫結至物件的型別轉換子。

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

範例

以下範例宣 MyClass 稱使用稱為 MyClassConverter的型別轉換器。 此範例假設 已 MyClassConverter 在其他地方實作。 實作轉換器(MyClassConverter)的類別必須繼承該 TypeConverter 類別。

[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

下一個例子會產生一個 的 MyClass實例。 接著取得類別的屬性,並列印出所 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();

    // 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

備註

你用於轉換的類別必須繼承自 TypeConverter。 使用該 ConverterTypeName 屬性取得提供該屬性綁定物件資料轉換的類別名稱。

如需屬性的詳細資訊,請參閱 屬性。 欲了解更多關於型別轉換器的資訊,請參閱 TypeConverter 基礎類別及 《如何實作型別轉換器》。

為了在自訂類別中建立一個型別轉換器,提供 XAML 型別轉換行為,你需要將該 TypeConverterAttribute 屬性套用到你的型別上。 屬性的參數指向你的型別轉換器實作。 你的型別轉換器應該能接受用於屬性或 XAML 標記中初始化文字的字串,並將該字串轉換成你想要的目標型別。 欲了解更多資訊,請參閱 TypeConverters 與 XAML

與其套用於所有型別值,XAML 的型別轉換器行為也可以建立在特定屬性上。 在這種情況下,你是應用TypeConverterAttribute於屬性定義(外部定義,而非特定get定義)。set

可透過套用TypeConverterAttributeget到 XAML 的方法存取器來指派一種類型轉換器行為,以支援該 XAML 的使用。 如需詳細資訊,請參閱附加屬性概觀

對於需要物件執行時額外狀態的複雜 XAML 序列化,可以考慮除了型別轉換器外,還要定義一個值序列化器,並在自訂型別或自訂成員上為兩個支援類別都設定屬性。 如需詳細資訊,請參閱ValueSerializer

建構函式

名稱 Description
TypeConverterAttribute()

初始化一個新的類別實例 TypeConverterAttribute ,使用預設型別轉換器,該轉換器為空字串(“”)。

TypeConverterAttribute(String)

初始化該類別的新實例 TypeConverterAttribute ,使用指定的類型名稱作為綁定該物件的資料轉換器。

TypeConverterAttribute(Type)

初始化該類別的新實例 TypeConverterAttribute ,使用指定的類型作為該屬性綁定物件的資料轉換器。

欄位

名稱 Description
Default

指定該屬性綁定的物件的轉換器類型。

屬性

名稱 Description
ConverterTypeName

取得 的 Type 完全限定型別名稱,作為該屬性綁定物件的轉換器。

TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

回傳給定物件的值是否等於當前 TypeConverterAttribute值。

GetHashCode()

傳回這個實例的哈希碼。

GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

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

擷取 物件的型別資訊,可用來取得介面的類型資訊。

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

擷取物件提供的類型資訊介面數目 (0 或 1)。

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

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱