How can I create a JsonPatchDocument from comparing two c# objects?

前端 未结 3 1776
醉话见心
醉话见心 2021-01-31 19:51

Given I have two c# objects of the same type, I want to compare them to create a JsonPatchDocument.

I have a StyleDetail class defined like this:

public         


        
3条回答
  •  生来不讨喜
    2021-01-31 20:32

    You can use this

    You can install using NuGet, see SimpleHelpers.ObjectDiffPatch at NuGet.org

    PM> Install-Package SimpleHelpers.ObjectDiffPatch
    

    Use:

    StyleDetail styleNew = new StyleDetail() { Id = "12", Code = "first" };
    StyleDetail styleOld = new StyleDetail() { Id = "23", Code = "second" };
    var diff = ObjectDiffPatch.GenerateDiff (styleOld , styleNew );
    
    // original properties values
    Console.WriteLine (diff.OldValues.ToString());
    
    // updated properties values
    Console.WriteLine (diff.NewValues.ToString());
    

提交回复
热议问题