使用WebService传递有继承关系的数组之解决方案

小弟我最近遇到一个棘手的问题.使用WebService返回普通数组没问题,返回基类数组没问题,返回子类数组也没问题。但是当我返回一个用基类引用指向子类的实例的数组时就会有问题。请高手指点迷津。
---------------------------------------------------------------

use XmlInclude, something like

1@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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus