TypeConverterAttribute 类

指定用作此属性所绑定到的对象的转换器的类型。无法继承此类。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class TypeConverterAttribute
    Inherits Attribute
用法
Dim instance As TypeConverterAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public sealed class TypeConverterAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class TypeConverterAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public final class TypeConverterAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public final class TypeConverterAttribute extends Attribute

备注

用于转换的类必须从 TypeConverter 继承。使用 ConverterTypeName 属性 (Property) 来获取为该属性 (Attribute) 所绑定到的对象提供数据转换的类名。

有关属性的更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据。有关类型转换器的更多信息,请参见 TypeConverter 基类和 如何:实现类型转换器

示例

下面的示例通知 MyClass 使用名为 MyClassConverter 的类型转换器。此示例假定已在其他位置实现了 MyClassConverter。实现转换器 (MyClassConverter) 的类必须从 TypeConverter 类继承。

<TypeConverter(GetType(MyClassConverter))> _
Public Class ClassA
    ' Insert code here.
End Class 'MyClass
[TypeConverter(typeof(MyClassConverter))]
 public class MyClass {
    // Insert code here.
 }
[TypeConverter(Class1::MyClassConverter::typeid)]
ref class MyClass{
   // Insert code here.
};
/** @attribute TypeConverter(MyClassConverter.class)
 */
public static class MyClass
{
    // Insert code here.
} //MyClass   

下一个示例创建 MyClass 的实例。然后,它获取该类的属性,并输出 MyClass 所用类型转换器的名称。

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
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;
 }
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 void main(String[] args)
{
    // 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.get_Item( TypeConverterAttribute.class.ToType())));
    Console.WriteLine(("The type conveter for this class is: " 
        + myAttribute.get_ConverterTypeName()));
   } //main 

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.TypeConverterAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

TypeConverterAttribute 成员
System.ComponentModel 命名空间
TypeConverter 类