鉴于我有两个相同类型的 c# 对象,我想比较它们以创建 JsonPatchDocument。
我有一个这样定义的 StyleDetail 类:
public class StyleDetail
{
public string Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal OriginalPrice { get; set; }
public decimal Price { get; set; }
public string Notes { get; set; }
public string ImageUrl { get; set; }
public bool Wishlist { get; set; }
public List<string> Attributes { get; set; }
public ColourList Colours { get; set; }
public SizeList Sizes { get; set; }
public ResultPage<Style> Related { get; set; }
public ResultPage<Style> Similar { get; set; }
public List<Promotion> Promotions { get; set; }
public int StoreStock { get; set; }
public StyleDetail()
{
Attributes = new List<string>();
Colours = new ColourList();
Sizes = new SizeList();
Promotions = new List<Promotion>();
}
}
如果我有两个 StyleDetail 对象
StyleDetail styleNew = db.GetStyle(123);
StyleDetail styleOld = db.GetStyle(456);
我现在想创建一个 JsonPatchDocument,这样我就可以将差异发送到我的 REST API... 怎么做?
JsonPatchDocument patch = new JsonPatchDocument();
// Now I want to populate patch with the differences between styleNew and styleOld - how?
在 javascript 中,有一个库可以做到这一点https://www.npmjs.com/package/rfc6902
计算两个对象之间的差异:
rfc6902.createPatch({first: 'Chris'}, {first: 'Chris', last: 'Brown'});
[ { op: 'add', path: '/last', value: 'Brown' } ]
但我正在寻找 ac# 实现