TypeConverterAttribute 类

定义

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

示例

下面的示例声明MyClass使用名MyClassConverter为的类型转换器。The 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实例。The next example creates an instance of MyClass. 然后, 它获取类的属性, 并输出使用MyClass的类型转换器的名称。Then 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).

可通过将应用TypeConverterAttribute get到支持 xaml 使用的方法访问器来分配自定义可附加成员的 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 类的新实例。Initializes a new instance of the TypeConverterAttribute class with the default type converter, which is an empty 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 类的新实例。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

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

属性

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

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

(继承自 Attribute)

方法

Equals(Object)

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

GetHashCode()

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

GetType()

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

(继承自 Object)
IsDefaultAttribute()

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

(继承自 Attribute)
Match(Object)

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

(继承自 Attribute)
MemberwiseClone()

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

(继承自 Object)
ToString()

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

(继承自 Object)

显式界面实现

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

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

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

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

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

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

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

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

(继承自 Attribute)

适用于

另请参阅