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

備註

您用於轉換的類別必須繼承自 TypeConverterConverterTypeName使用 屬性來取得類別的名稱,這個類別會提供這個屬性所系結物件的資料轉換。

如需屬性的詳細資訊,請參閱屬性。 如需類型轉換器的詳細資訊,請參閱 TypeConverter 基類和 如何:實作類型轉換器

若要在提供 XAML 類型轉換行為的自訂類別上建立類型轉換器,請將 屬性套用 TypeConverterAttribute 至類型。 屬性的引數會參考您的類型轉換器實作。 您的類型轉換子應該能夠接受字串中的值,該字串用於 XAML 標記中的屬性或初始化文字,並將該字串轉換成您想要的目的地類型。 如需詳細資訊,請參閱 TypeConverters 和 XAML

您也可以在特定屬性上建立 XAML 的類型轉換子行為,而不是套用至類型的所有值。 在此情況下,您會套用 TypeConverterAttribute 至外部定義 (屬性定義,而不是) 的特定 getset 定義。

您可以藉由將 套用 TypeConverterAttributeget 支援 XAML 用法的方法存取子,來指派自訂附加成員之 XAML 使用方式的類型轉換器行為。 如需詳細資訊,請參閱附加屬性概觀

對於需要物件執行時間額外狀態的複雜 XAML 序列化案例,請考慮除了類型轉換器之外定義值序列化程式,而且屬性都支援自訂類型或自訂成員上的類別。 如需詳細資訊,請參閱ValueSerializer

建構函式

TypeConverterAttribute()

使用預設型別轉換子,也就是空字串 (""),來初始化 TypeConverterAttribute 類別的新執行個體。

TypeConverterAttribute(String)

使用指定型別名稱做為這個屬性所繫結之物件的資料轉換子,來初始化 TypeConverterAttribute 類別的新執行個體。

TypeConverterAttribute(Type)

使用指定型別做為這個屬性所繫結之物件的資料轉換子,來初始化 TypeConverterAttribute 類別的新執行個體。

欄位

Default

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

屬性

ConverterTypeName

取得 Type 的完整型別名稱來做為這個屬性所繫結之物件的轉換子。

TypeId

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

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,表示指定的物件值是否等於目前的 TypeConverterAttribute

GetHashCode()

傳回這個執行個體的雜湊碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

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

適用於

另請參閱