继承的问题.高手请进

njx 2005-02-05 01:30:28
小弟我最近遇到一个棘手的问题.使用WebService返回普通数组没问题,返回基类数组没问题,返回子类数组也没问题。但是当我返回一个用基类引用指向子类的实例的数组时就会有问题。请高手指点迷津。
...全文
125 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2005-02-08
  • 打赏
  • 举报
回复
use XmlInclude, something like


<%@WebService class="MyServices" language="C#"%>

using System;
using System.Web.Services;
using System.Xml.Serialization;

[Serializable]
[XmlInclude(typeof(GroundInfo))]
public class TileInfo
{
public int i;
}

[Serializable]
public class GroundInfo : TileInfo
{
public string s;
}


[WebService(Description="Test Service", Namespace="http://csdn.net/aspnet")]
public class MyServices
{
[WebMethod]
public TileInfo[] GetGround()
{
TileInfo[] tiles = new TileInfo[2];

tiles[0] = new TileInfo();
tiles[0].i = 100;

tiles[1] = new GroundInfo();
tiles[1].i = 200;
((GroundInfo)tiles[1]).s = "hello";
return tiles;
}
}



also see

Serializing Derived Classes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrollingserializationbyxmlserializerwithattributes.asp


njx 2005-02-08
  • 打赏
  • 举报
回复
回复:lisoft2002(lisoft2002)
我不明白你要进行的试验的用意,不过
子类的引用不能指向基类的实例,这是基本常识。

还是希望有见地的高手帮我一把。我想这个问题应该没那么难。
kenMoxi 2005-02-07
  • 打赏
  • 举报
回复
up
好运 2005-02-07
  • 打赏
  • 举报
回复
返回一个用子类引用指向基类的实例的数组时看看有没有问题
yizhixiaozhu 2005-02-06
  • 打赏
  • 举报
回复
不会
帮你up
njx 2005-02-06
  • 打赏
  • 举报
回复
[WebMethod]
public TileInfo[] GetGround()
{
GroundInfo[] grounds = new GroundInfo[2];
grounds[0] = new GroundInfo();
grounds[1] = new GroundInfo();
return grounds;
}
其中 TileInfo 是 GroundInfo 的基类,这样写就不行。感兴趣的朋友可以自己试一下。但是编译不会出错。希望高手帮忙。
njx 2005-02-06
  • 打赏
  • 举报
回复
高手帮忙呀,我想这应该是个普遍的问题。既然编译没有错误那么我想应该有办法解决的。WebService不会那么弱吧。
AllenTing 2005-02-06
  • 打赏
  • 举报
回复
cqzhangq 2005-02-05
  • 打赏
  • 举报
回复
up

12,165

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧