通过


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 定义)。

可以通过向支持 XAML 用法的方法访问器应用TypeConverterAttributeget支持 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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,该信息可用于获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅