Search code examples
c#castingboxingunboxing

C# Type Conversion


I have two objects. Object A and Object B.

Object A is an instance of a class that was generated from several XSD files. Used xsd.exe /c and compiled them. Now I have my new object.

I also have a web service, returning something very similar to object A. So right now I have something along the lines of this:

WebService.foo myResponseObj = MyService.GetObject(inData);
MyFramework.foo myClientObj = new MyFramework.foo();

What I want to do is this

myClientObj = (MyFramework.foo)myResponseObj

However, it's not really liking this. Says "Cannot implicitly convert MyFramework.foo[] to WebService.foo[]

Any ideas on how to resolve this? The object is quite large and they are basically identical.


Solution

  • How about extracting the interface (right click on one class, select Refactor->Extract Interface), and apply this interface to both classes?

    So it will look something like:

    namespace WebService
    {
       public class foo : IExtractedInterface
    }
    

    and

    namespace MyFramework
    {
       public class foo : IExtractedInterface
    }
    

    You should then be able to do:

    IExtractedInterface myClientObj = (IExtractedInterface)myResponseObj;