TypeConverterAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定对于此属性绑定到的对象要使用哪种类型作为转换器。
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 AttributePublic NotInheritable Class TypeConverterAttribute
Inherits Attribute- 继承
- 属性
示例
以下示例声明 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 定义)。
可以通过将 TypeConverterAttribute 应用于支持 XAML 用法的 get 方法访问器,来分配自定义可附加成员的 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) |