详解如何动态调用WebService传递复杂对象的实现

本文介绍了一种在动态调用WebService时传递复杂对象的方法。通过序列化和反序列化复杂对象,解决了动态代理无法直接调用的问题。文章提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                       详解如何动态调用WebService传递复杂对象的实现

项目需要用到外部提供的WebService,由于WebService的地址不固定,因此,采用动态调用的方式。

动态代理会自动生成自己的对象,无法通过强制转换实现调用。

解决办法:

1 传递对象为简单类型(string)

调用webservice方法成功之后,返回对象之前,对复杂对象进行xml序列化

返回对象中包含List结构

1:[Serializable]2:publicclassQuestionEditList : BaseModel3:{4:privateList _questionEditList =newList();5: 6:publicListQuesEditList7:{8:get {return_questionEditList ; }9:set { _questionEditList =value; }10:}11:publicQuestionEditList(ListquestionEditList)12:{13:this._questionEditList= questionEditList;14: 15:}16:publicoverridevoidFromXml(stringxml)17:{18:QuestionEditList question = (QuestionEditList)base.DeserializeFromXml(xml,typeof(QuestionEditList));19:this.QuesEditList = question.QuesEditList;20:}21: 22:publicoverridestringToXml()23:{24:returnbase.SerializeToXml(this);25:}26:}序列化后的结果:102030

2 调用方拿到返回值,在进行xml反序列化

1:QuestionEditList questionList =newQuestionEditList();2:questionList.FromXml(obj.ToString());通过这样的方式传递,就可以获取到可以控制的对象。 序列化,反序列化方法1 序列化

1:protectedstringSerializeToXml(objecto)2:{3:XmlSerializer serializer =newXmlSerializer(o.GetType(),"");4:MemoryStream w =newMemoryStream();5:XmlTextWriter writer =newXmlTextWriter(w, Encoding.Default);6:writer.Formatting = Formatting.None;7:serializer.Serialize((XmlWriter)writer, o);8:writer.Close();9:returnEncoding.Default.GetString(w.ToArray()).Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"","").Replace("","");10:}

2 反序列化

1:protectedobjectDeserializeFromXml(strings, Type t)2:{3:XmlSerializer serializer =newXmlSerializer(t);4:objectobj2 =null;5:XmlTextReader xmlReader =newXmlTextReader(s, XmlNodeType.Element,null);6:obj2 = serializer.Deserialize(xmlReader);7:xmlReader.Close();8:returnobj2;9:}

原文链接:http://code.01yun.com/asp0dm/asp0net0dm/20110604/52724.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值