Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TypeConverterAttribute Class

Specifies what type to use as a converter for the object this attribute is bound to.

System::Object
  System::Attribute
    System.ComponentModel::TypeConverterAttribute

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

[AttributeUsageAttribute(AttributeTargets::All)]
public ref class TypeConverterAttribute sealed : public Attribute

The TypeConverterAttribute type exposes the following members.

  NameDescription
Public methodTypeConverterAttribute()Initializes a new instance of the TypeConverterAttribute class with the default type converter, which is an empty string ("").
Public methodTypeConverterAttribute(String)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.
Public methodTypeConverterAttribute(Type)Initializes a new instance of the TypeConverterAttribute class, using the specified type as the data converter for the object this attribute is bound to.
Top

  NameDescription
Public propertyConverterTypeNameGets the fully qualified type name of the Type to use as a converter for the object this attribute is bound to.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top

  NameDescription
Public methodEqualsReturns whether the value of the given object is equal to the current TypeConverterAttribute. (Overrides Attribute::Equals(Object).)
Public methodGetHashCodeReturns the hash code for this instance. (Overrides Attribute::GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public fieldStatic memberDefaultSpecifies the type to use as a converter for the object this attribute is bound to.
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute::GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute::GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute::GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute::InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

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 Extending Metadata Using Attributes. For more information about type converters, see the TypeConverter base class and How to: Implement a Type Converter.

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. 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. For more information, seeTypeConverters and XAML.

Rather than applying to all values of a type, a type converter behavior for XAML can also be established on a specific property. In this case, you apply TypeConverterAttribute to the property definition (the outer definition, not the specific get and set definitions).

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.

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. For more information, see ValueSerializer.

The following example declares 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.

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

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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