开发者

ASP.NET Web Service return arraylist of different object types

开发者 https://www.devze.com 2023-04-09 15:05 出处:网络
I got a problem when I tried ASP.NET WebService to return arraylist of several object types. Suppose I have a Book object and a Table object.

I got a problem when I tried ASP.NET WebService to return arraylist of several object types. Suppose I have a Book object and a Table object. I added Book and开发者_如何转开发 Table objects to an ArrayList. After that I return that arraylist in webservice. It does not allow me to do. How can I make it be able to return several object types?


Can you not define a complex object (DTO in this examle) that contains the other objects and return the populated DTO in you webmethod:

[OperationContract]
Dto GetBooksAndTables();


[DataContract]
public class Dto
{
    [DataMember]
    public Book[] Books { get; set; }

    [DataMember]
    public Table[] Tables { get; set; }
}

[DataContract]
public class Book 
{
   [DataMember]
   public string BookName {get; set; }
   //etc...
}

[DataContract]
public class Table 
{
   [DataMember]
   public string TableName {get; set; }
   //etc...
}

Have you looked at http://wcf.codeplex.com/wikipage?title=WCF%20HTTP - it makes building .NET services much easier. It's on NuGet.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号