Export (0) Print
Expand All

TypeConverterAttribute Class

Specifies what type to use as a converter for the object this attribute is bound to. This class cannot be inherited.

For a list of all members of this type, see TypeConverterAttribute Members.

System.Object
   System.Attribute
      System.ComponentModel.TypeConverterAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.All)>
NotInheritable Public Class TypeConverterAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.All)]
public sealed class TypeConverterAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::All)]
public __gc __sealed class TypeConverterAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.All)
class TypeConverterAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The class you use for conversion must inherit from TypeConverter. 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 Overview and Extending Metadata Using Attributes. For more information about type converters, see the TypeConverter base class and Implementing a Type Converter.

Example

[Visual Basic, C#, C++] The following example tells MyClass to use the type converter called MyClassConverter. This example assumes that MyClassConverter has been implemented elsewhere. The class implementing the converter (MyClassConverter) must inherit from the TypeConverter class.

[Visual Basic] 
<TypeConverter(GetType(MyClassConverter))> _
Public Class ClassA
    ' Insert code here.
End Class 'MyClass

[C#] 
[TypeConverter(typeof(MyClassConverter))]
 public class MyClass {
    // Insert code here.
 }

[C++] 
public:
    [TypeConverter(__typeof(Class1::MyClassConverter))]
    __gc class MyClass {
        // Insert code here.
    };

[Visual Basic, C#, C++] The next example creates an instance of MyClass. Then it gets the attributes for the class, and prints the name of the type converter used by MyClass.

[Visual Basic] 
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

[C#] 
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;
 }

[C++] 
int main() {
    // Creates a new instance of MyClass.
    Class1::MyClass* myNewClass = new 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->Item[__typeof(TypeConverterAttribute)]);

    Console::WriteLine(S"The type converter for this class is: {0}", myAttribute->ConverterTypeName);

    return 0;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

TypeConverterAttribute Members | System.ComponentModel Namespace | TypeConverter

Show:
© 2014 Microsoft