Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explicit casting and shared dlls

Status
Not open for further replies.

serializer

Programmer
May 15, 2006
143
0
0
SE
I have a dll which has some base classes - for example a class called Market.

This dll is used in a web service and a winform project. In the webservice I have a function returning and object of type Market.

The problem I am having is that I can't cast the returned object to the dll class. I have tried (Market) and as conversion but get errors at compile level.

Even though the classes are "the same" I assume that the namespace is the problem. An example of this is that the winforms application thinks that the webservice function returns type:

Winformsapplication.Servicereference.Market

While the dll that both uses are:

MyDll.Market

How can I convert these objects?
 
they are not the same. it like meeting two people with the same name, they are not the same person. You have 2 options.
1. use an adapter
Code:
class MarketAdapter : Winformsapplication.Servicereference.Market
{
   private readonly MyDll.Market market;
   public MarketAdapter(MyDll.Market market)
   {
       this.market = market;
   }

   public Id 
   { 
      get { return market.Id;} 
      set {market.Id = value;} 
   }

   //repeat for all properties
}
2. map one to the other
Code:
class MarketMapper
{
   public Winformsapplication.Servicereference.Market map_from(Dll.Market market)
   {
         return new Winformsapplication.Servicereference.Market
             {
                Id = market.Id,
                //repeate for other properties
             };
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Serializer,

Another approach I often take is to return a string rather than an object from my WebService. This string is a serialized Market (i.e. xml representation).

I then deserialize this string on the client side to type Market. This then gives you the same type on both client and server, given that the class is defined in a DLL used on both client and server, this will work fine.

Let me know if you need a more details example.

Thanks,

madJock

"Just beacuse you're paranoid, don't mean they're not after you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top