Groups | Blog | Home
all groups > dotnet windows forms designtime > august 2004 >

dotnet windows forms designtime : TypeDescriptor.GetConverter


kw
8/22/2004 8:23:22 PM
I'm getting different behavior from what I would expect and was hoping
someone could clue me in. At run time I need to examine a property of an
object for a custom TypeConverter, then use that converter on a value.

Here's the deal:

object[]
attrs=(object[])PropertyInfo.GetCustomAttributes(typeof(TypeConverterAttribu
te),false);

TypeConverterAttribute a = (TypeConverterAttribute)attrs[0];

Type t=Type.GetType(a.ConverterTypeName);

//ok up to this point. Note that Type t is in an external assembly (it
shouldn't matter)

TypeConverter tc=TypeDescriptor.GetConverter(t);

Debug.WriteLine(tc.ConvertToString(this,vValue));

//it didn't work?! The TypeConverter never got invoked.

I'm getting different behavior from what I would expect and was hoping
someone could clue me in. At run time I need to examine a property of an
object for a custom TypeConverter, then use that converter on a value.

Not that it should matter, but the TypeConverter I use looks like this:
public class FormatCurrency : StringConverter{...}

And the object property looks like this:

[TypeConverter(typeof(MyNameSpace.FormatCurrency))]

public Double CurrencyWithTypeConverter{..}

kw
8/23/2004 9:13:34 AM
I found the solution:

TypeConverter tc=(TypeConverter)Activator.CreateInstance(t);

GetConverter() was the wrong approach.

AddThis Social Bookmark Button