Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i've got two classes with exactly the same properties and methods, basically the source code is copied and yes, i know thats the design problem ;)

I cannot flip the whole design over night and the questions sounds pretty interesting to me ;)

Is it possible to "cast" a object of ClassA to ClassB, as i already mentioned the classes are the same, only the name is different.

Tryed LinFu.Reflection.DynamicObject -> CreateDuck already but it looses all properties (am i doing something wrong?).

The .NET 4.0 Keyword dynamic doesn't help me either.

Regards,
Kjartan
Posted

If they are truly identical, then yes, I would ditch one of them completely.

If for whatever reasons you can't, then rather than perpetuate the problem I would be tempted to create a new third class witch contains all the properties and methods, then derive both of the previous ones from that. Each of them could be cast "down" to the source without losing any info, and the source class used in all situations when the two come together.

Then slowly, the two separate classes could be replaced with the source and deleted.
 
Share this answer
 
Comments
Shahin Khorshidnia 3-Jan-12 5:02am    
5+
I've just got the problem from my team and as it turned out we cannot touch these classes at all as they both come from webservices we dont have sources from.

So the problem: two webservices, returning the same data in different named objects and i want an automated meachanism to cast one object into the other.
Thought about XML (de)serialization and some manipulation in between already, gonna give it a try ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900