TypeConverter::ConvertFrom ignores implicit cast

N

not_a_commie

I have an Angle class that I store angles in. It's basically just a
bunch of fancy functions for manipulating a double. It has implicit
casts for converting to/from double. Due to the project structure and
where it stores the preferred SI units for display, I wanted to have
the TypeConverter in a different project that has no reference to the
angle class. However, returning a double from the ConvertFrom function
throws an error that it can't convert from "System.Double" to my Angle
type. Originally, before the SI units where unaccessable, I had this
in there:

double angle;
...
if (context.PropertyDescriptor.PropertyType.Equals(typeof(Angle)))
return new Angle(angle);
return angle;

Can I do something similar even if I don't have a reference to the
Angle type? Why would it not accept my implicit cast to double?
 
D

Dave Sexton

Hi,

The problem is that the ConvertFrom method returns an object, not a double.
The runtime will only call your implicit cast operator overload for double
if the value being cast isn't boxed.

Unfortunately, there isn't much you can do here since you can't overload the
implicit cast operator from Object.

Your best bet is to make the Angle class, which BTW should probably be a
struct, visible to the TypeConverter class that requires it. It's strange
to separate them unless you're planning on using the same TypeConverter
class to convert values other than just your Angle type, but that wouldn't
make much sense either.
 

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