TypeConverterAttribute TypeConverterAttribute TypeConverterAttribute TypeConverterAttribute Class

定义

指定对于此属性绑定到的对象要使用哪种类型作为转换器。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
继承
TypeConverterAttributeTypeConverterAttributeTypeConverterAttributeTypeConverterAttribute
属性

示例

下面的示例声明MyClass若要使用名为的类型转换器MyClassConverterThe 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 'MyClass

下一个示例创建的实例MyClassThe next example creates an instance of MyClass. 然后,它获取的特性类,并输出由类型转换器名称MyClassThen 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).

可以通过将应用分配的自定义的可附加成员的 XAML 用法的类型转换器行为TypeConverterAttributeget支持 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() TypeConverterAttribute() TypeConverterAttribute()

使用默认类型转换器,即空字符串("")初始化 TypeConverterAttribute 类的新实例。Initializes a new instance of the TypeConverterAttribute class with the default type converter, which is an empty string ("").

TypeConverterAttribute(String) TypeConverterAttribute(String) TypeConverterAttribute(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(Type) TypeConverterAttribute(Type) 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 Default Default Default

指定用作此特性所绑定到的对象的转换器的类型。Specifies the type to use as a converter for the object this attribute is bound to.

属性

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

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回给定对象的值是否等于当前的 TypeConverterAttributeReturns whether the value of the given object is equal to the current TypeConverterAttribute.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

适用于

另请参阅