一千萬個為什麽

搜索

我試圖從webmethod返回列表<object>,但給出錯誤

System.InvalidOperationException: There was an error generating the xml document. ---> System.InvalidOperationException: The type WebService.Property.Property_Users was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write8_ArrayOfAnyType(Object o) at Microsoft.Xml.Serialization.GeneratedAssembly.ListOfObjectSerializer.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.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o) 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 List GetDataByModuleName(string ModuleName)
    {
       List obj_UserList = new List(); //performing some operation that add data to obj_UserList List < Object > myList = new List(); return ConvertToObjectList(obj_UserList); } public List ConvertToObjectList(List sourceList) { List result = new List(); foreach (N item in sourceList) { result.Add(item as Object); } return result; } [WebMethod] public List GetDataByModuleName(string ModuleName) { List obj_list = new List(); obj_list = BAL_GeneralService.GetDataByModuleName(ModuleName); return obj_list; } 

最佳答案

The serializer cannot serialize object

如果要返回任意類型的列表,則必須使用XmlInclude或SoapInclude屬性為序列化程序提供提示。

Also, the list must be homogenous, containing elements of only one type. e.g. you cannot return a list containing a Foo and a Bar

轉載註明原文: 我試圖從webmethod返回列表<object>,但給出錯誤