List 带有附加属性的Web API扩展列表

List 带有附加属性的Web API扩展列表,list,generics,asp.net-web-api,List,Generics,Asp.net Web Api,我喜欢在列表级别扩展具有附加属性的项目列表。 因此,我可以给出列表的名称、分页信息等 这是列表的示例对象项: public class House { public int Nummer { get; set; } public string Name { get; set; } } 这是我的简单列表类-具有一个附加属性: public class SimpleList : List<House> { public string MyExtraProperty

我喜欢在列表级别扩展具有附加属性的项目列表。 因此,我可以给出列表的名称、分页信息等

这是列表的示例对象项:

public class House
{
    public int Nummer { get; set; }
    public string Name { get; set; }
}
这是我的简单列表类-具有一个附加属性:

public class SimpleList : List<House>
{
  public string MyExtraProperty { get; set; }
}
结果显示在XML中:

<ArrayOfHouse>
 <House><Name>Name of House</Name><Nummer>1</Nummer></House>
 <House><Name>Name of House</Name><Nummer>2</Nummer></House>
</ArrayOfHouse>


谢谢你的帮助

最简单的方法是让列表成为SimpleList的成员而不是超类

public class SimpleList 
{
    public List<House> Houses;
    public string MyExtraProperty { get; set; }
}
公共类SimpleList
{
上市公司;
公共字符串MyExtraProperty{get;set;}
}
如果您只需要JSON,您可以通过为Newtonsoft JSON修饰模型来控制序列化:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApiGenerics.Models
{
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class SimpleList : List<House>
    {
        [JsonProperty]
        public IEnumerable<House> Houses
        {
            get { return this.Select(x => x); }
        }

        [JsonProperty]
        public string MyExtraProperty { get; set; }
    }
}
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
名称空间WebApiGenerics.Models
{
[JsonObject(MemberSerialization=MemberSerialization.OptIn)]
公共类SimpleList:列表
{
[JsonProperty]
公屋
{
获取{返回这个。选择(x=>x);}
}
[JsonProperty]
公共字符串MyExtraProperty{get;set;}
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApiGenerics.Models
{
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class SimpleList : List<House>
    {
        [JsonProperty]
        public IEnumerable<House> Houses
        {
            get { return this.Select(x => x); }
        }

        [JsonProperty]
        public string MyExtraProperty { get; set; }
    }
}