Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
TypeConverterAttribute Class

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

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)
Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class TypeConverterAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As TypeConverterAttribute
C#
[AttributeUsageAttribute(AttributeTargets.All)]
public sealed class TypeConverterAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::All)]
public ref class TypeConverterAttribute sealed : public Attribute
JScript
public final class TypeConverterAttribute extends Attribute

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 How to: Implement a Type Converter.

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.
 }

Visual C++
[TypeConverter(Class1::MyClassConverter::typeid)]
ref class MyClass{
   // Insert code here.
};

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;
 }

Visual C++
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;
}

System..::.Object
  System..::.Attribute
    System.ComponentModel..::.TypeConverterAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker