PDA

View Full Version : Type Converter Question


ash9696
02-07-2007, 12:08 AM
hi there,

I am working on a project where i have a lot of instances where i am trying to convert between two user types according to some custom logic.

I am wondering if i could use spring's type conversion utility, where i can then replace type conversion logic if needed.

I am mostly using prototype objects, so for example i would have to do something like

class supervisor
{}

class manager{}

and in the code

supervisor super = new supervisor(); // create instance from spring...
manager manage = super; // somehow convert it

its not very clear from reading the documentation if i can do this, and how this can be done in the code, although i can see how i can do this from configuration file

can someone post a small example on how this might be accomplished ?

Thanks
-ashish

Erich Eichinger
02-07-2007, 09:12 AM
Hi,

you could give Spring.Util.ConversionUtils.ConvertValueIfNecessar y() a try. Of course you somehow need to register a custom converter for SuperVisor->Manager (altough I personally don't think, that such a conversion is really possible - one might only wrap a supervisor within a manager *bg* ) - see the type conversion (http://www.springframework.net/doc-latest/reference/html/objects.html#objects-type-conversion) chapter in the reference documentation.

Then you could call


manager = ConversionUtils.ConvertValueIfNecessary( typeof(Manager), super, "manager" );


You can also make use .NET's built in type conversion mechanism. Look for "conversion operators" in the MSDN docs.

cheers,
Erich