How to instanciate a TypeConverter from an Attribute ?

  • Thread starter Thread starter Herve Bocuse
  • Start date Start date
H

Herve Bocuse

Hi,

I have a boolean variable like this:

[TypeConverter(typeof(MyBooleanConverter))]
private bool var = false;
From somewhere else in the code, I can access this variable only thanks
to a memberInfo. To retrieve the attribute I do:

TypeConverterAttribute attrib =
(TypeConverterAttribute)Attribute.GetCustomAttribute(_memberInfo,
typeof(TypeConverterAttribute));

But now I don't now how to create the proper TypeConverter instance
from attrib.

Thank you for your help

Herve
 
Hi,
You can query for TypeConverterAttribute.ConverterTypeName which
returns the full name of the custom type converter and the load the
same type using Type.GetType( <typename>).
Here I'm adding the code snippet for instantiating a custome type
converter from TypeConverter attribute

TypeConverterAttribute attrib =
(TypeConverterAttribute)Attribute.GetCustomAttribute(_member­Info,
typeof(TypeConverterAttribute));
MyBooleanConverter attr =
(MyBooleanConverter)Activator.CreateInstance(Type.GetType(attrib.ConverterTypeName),
false);
 
Back
Top