我正在尝试在 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 的访问修饰符。