Convert object to a specific type

  • Thread starter Thread starter Peter Morris
  • Start date Start date
P

Peter Morris

I will be receiving an "object" reference from one method, and a "Type"
reference from another. I need to

A: Check that I can convert the object to the type
B: If I can, then convert it

Is there something I can already use to do this, or am I going to have to
write a lot of code?


Thanks
 
Peter,

You can do A (and I believe you mean cast, not convert, that's a
different operation), but you can't do B. The code has to know what the
type is at compile time in order for you to do anything with it (without
using Reflection).

With A, you can use the IsAssignable from to determine if you can assign
a variable of one type to another.

What exactly are you trying to do with B? Perhaps there is a different
way to achieve what you are trying to do.
 
I have a class structure something like this

Key:
[ClassName]
(RoleName)

[Template] (Template) 1----* (Properties) [TemplateProperty]


TemplateProperty is abstract. I have various subclasses of
TemplateProperty. Boolean, Numeric, String for example.

I import this structure from a file on disk. If at some point in the
future the user re-imports the file I need to update the structure. I
update the Property.DefaultValue and so on. Problem is when the user
changes the property type. Changing from Numeric to String for example is
pretty simple, but String to Boolean I would have to check.

I think I am going to have to just implement a few methods on the base
property class, something like

MyExistingProperty.CreateMigratedProperty(typeOfPropertyToConvertTo)

and have each property class override it. I thought that would be the best
way, it gives me more control at least :-)


Thanks for the info.
 
Peter said:
I have a class structure something like this

Key:
[ClassName]
(RoleName)

[Template] (Template) 1----* (Properties) [TemplateProperty]


TemplateProperty is abstract. I have various subclasses of
TemplateProperty. Boolean, Numeric, String for example.

I import this structure from a file on disk. If at some point in the
future the user re-imports the file I need to update the structure. I
update the Property.DefaultValue and so on. Problem is when the user
changes the property type. Changing from Numeric to String for example
is pretty simple, but String to Boolean I would have to check.

I think I am going to have to just implement a few methods on the base
property class, something like

MyExistingProperty.CreateMigratedProperty(typeOfPropertyToConvertTo)

and have each property class override it. I thought that would be the
best way, it gives me more control at least :-)
IConvertible and Convert already have this covered as far as primitive types go.

Implementing IConvertible should only be done for converting types from and
to primitive types, by the way, not to other arbitrary types
(BooleanTemplateProperty to bool and string and back, but not to
StringTemplateProperty -- use your own methods for that if you need it). I
mention this here because people seem to naturally assume that these classes
should be used for any kind of conversion, but that doesn't really work.
 
Nicholas said:
Peter,

You can do A (and I believe you mean cast, not convert, that's a
different operation), but you can't do B. The code has to know what
the type is at compile time in order for you to do anything with it
(without using Reflection).

With A, you can use the IsAssignable from to determine if you can
assign a variable of one type to another.

What exactly are you trying to do with B? Perhaps there is a
different way to achieve what you are trying to do.

There's a substantial TypeConverter framework for design-time support that
could probably help solve this problem.
 
Back
Top