Unexpected behavior: TypeConverter tc=TypeDescriptor.GetConverter(t);

K

kw

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

kw

No, this is not the issue. Note that this line executes correctly:
Type t=Type.GetType(a.ConverterTypeName);

Thanks for trying :)

Sijin Joseph said:
If the TypeConverter is in a different assembly then you should use the
string overlaod of the TypeConverterAttribute ctor.

[TypeConverter("MyNameSpace.FormatCurrency")]
public Double CurrencyWithTypeConverter{..}

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


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

kw

I found the solution:

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

GetConverter() was the wrong approach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top