using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;namespace WebApplication_Test
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{ [WebMethod]
[XmlInclude(typeof(Felix))]
public Man ShowMan(string name)
{
Man felix = new Felix()
{
Name = string.Format("我的名字叫:{0}", name),
Hand = "一双手",
Body = "一个身体",
Foot = "一双脚",
MyFitstBook = new ThinkPad() { Brand = "ThinkPad", Price = 12000.6m },
MySecondBook = new 神舟笔记本() { Brand = "神舟电脑", Price = 3000m }
};
return felix;
}
}
public abstract class Man
{
public string Hand { get; set; }
public string Body { get; set; }
public string Foot { get; set; }
} public abstract class Book
{
public string Brand { get; set; }
}
public class ThinkPad:Book
{
public decimal Price { get; set; }
}
public class 神舟笔记本 : Book
{
public decimal Price { get; set; }
}
public class Felix : Man
{
public string Name { get; set; }
public Book MyFitstBook { get; set; }
public Book MySecondBook { get; set; }
}
}运行调用接口发生错误:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type WebApplication_Test.ThinkPad was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Book(String n, String ns, Book o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_Felix(String n, String ns, Felix o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_Man(String n, String ns, Man o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_Man(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.ManSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
==========================
求高手支招!!!
public Felix ShowMan(string name)
如果felix类中没有那个抽象的book类,就是可以序列化成功的
?