0

我正在尝试在 web api 中返回这个类:

    public IEnumerable<Product> Get()
    {
        var fruits = new Category("Fruits");
        var veggies = new Category("Veggies");

        var apple = new Product("apple");
        apple.Categories = new List<Category>() { fruits };

        var potato = new Product("Potatoes");
        potato.Categories = new List<Category>() { veggies };

        var banana = new Product("Banana");
        banana.Categories = new List<Category>() { fruits };

        List<Product> list = new List<Product>(){
          apple, potato, banana
        };

        return list; 
    }

无论出于何种原因,类别集合总是返回空......我错过了什么吗?

谢谢!

更新:

 public class Category
    {
        public string Id { get; private set; }
        public string Name { get; private set; }

        public Category() { }

        public Category(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("name must be set");

            this.Name = name;
        }
    }

Aaand.. 它不会显示类别,因为 Category 类中的两个属性都没有公共设置器。

让我想知道为什么序列化程序正在检查 setter 的访问修饰符。

4

2 回答 2

0

类别类有 2 个属性,都具有私有设置器;带有私有 setter 的属性不会被序列化。

于 2012-11-13T09:25:50.103 回答
0

我已验证私有集问题已在 web api RTM 版本中得到修复。

于 2012-11-13T19:13:00.790 回答