假设我有两个相同类型的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'});
但我正在寻找一个 C# 实现。
[ { op: 'add', path: '/last', value: 'Brown' } ]