ASP.NET Web 服务结果、代理类和类型转换 [英] ASP.NET Web Service Results, Proxy Classes and Type Conversion

查看:28
本文介绍了ASP.NET Web 服务结果、代理类和类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是 ASP.NET 世界的新手,所以我在这里可能会偏离基础,但到目前为止,这是我(有限的)知识中最好的!

I'm still new to the ASP.NET world, so I could be way off base here, but so far this is to the best of my (limited) knowledge!

假设我在 Business 命名空间中有一个标准的业务对象联系人".我编写了一个 Web 服务来从数据库中检索联系人的信息并返回它.然后我编写了一个客户端应用程序来请求所述详细信息.

Let's say I have a standard business object "Contact" in the Business namespace. I write a Web Service to retrieve a Contact's info from a database and return it. I then write a client application to request said details.

现在,我还创建了一个实用方法,它接受一个联系人"并用它做一些魔术,就像 Utils.BuyContactNewHat() 说的那样.这当然需要 Business.Contact 类型的 Contact.

Now, I also then create a utility method that takes a "Contact" and does some magic with it, like Utils.BuyContactNewHat() say. Which of course takes the Contact of type Business.Contact.

然后我返回到我的客户端应用程序并希望使用 BuyContactNewHat 方法,所以我添加了对我的 Utils 命名空间的引用,它就在那里.但是,出现了一个问题:

I then go back to my client application and want to utilise the BuyContactNewHat method, so I add a reference to my Utils namespace and there it is. However, a problem arises with:

Contact c = MyWebService.GetContact("Rob);
Utils.BuyContactNewHat(c); // << Error Here

因为 GetContact 的返回类型是 MyWebService.Contact 而不是预期的 Business.Contact.我理解这是为什么,因为在访问 Web 服务时,您实际上是针对 WSDL 生成的代理类进行编程.

Since the return type of GetContact is of MyWebService.Contact and not Business.Contact as expected. I understand why this is because when accessing a web service, you are actually programming against the proxy class generated by the WSDL.

那么,有没有更简单"的方法来处理这种类型的不匹配?我正在考虑尝试创建一个通用转换器类,该类使用反射来确保两个对象具有相同的结构,而不是简单地将值从一个传递到另一个.

So, is there an "easier" way to deal with this type of mismatch? I was considering perhaps trying to create a generic converter class that uses reflection to ensure two objects have the same structure than simply transferring the values across from one to the other.

推荐答案

您走对了.要将代理对象中的数据返回到您自己的对象之一,您必须执行左右手代码.即复制属性值.我敢打赌,已经有一个使用反射的通用方法.

You are on the right track. To get the data from the proxy object back into one of your own objects, you have to do left-hand-right-hand code. i.e. copy property values. I'll bet you that there is already a generic method out there that uses reflection.

如果某些人只想通过网络获取业务对象,他们会使用 Web 服务(.net 远程处理)以外的其他服务.或者他们将使用二进制序列化.我猜您使用 Web 服务是有原因的,因此您必须进行属性复制.

Some people will use something other than a web service (.net remoting) if they just want to get a business object across the wire. Or they'll use binary serialization. I'm guessing you are using the web service for a reason, so you'll have to do property copying.

这篇关于ASP.NET Web 服务结果、代理类和类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆