Skip to content

Instantly share code, notes, and snippets.

@jorik041
Forked from karenpayneoregon/Country.cs
Created January 30, 2024 22:50
Show Gist options
  • Save jorik041/66d007edd81c776f94a5ab9e921a2bd9 to your computer and use it in GitHub Desktop.
Save jorik041/66d007edd81c776f94a5ab9e921a2bd9 to your computer and use it in GitHub Desktop.
Basic example for CompareNETObjects NuGet package

Basic code to try out CompareNETObjects NuGet package for comparing objects.

Note 1

The following line excludes Human.Id and Country.Id from the compare operation

compare.Config.MembersToIgnore.Add("*Id");

Note 2

System.Text.Json is used to serialize bad results.

public class Country
{
public int Id { get; set; }
public string CountryName { get; set; }
}
public class Human
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateOnly BirthDay { get; set; }
public Country Country { get; set; }
}
List<Country> countries = [ new() { Id = 1, CountryName = "Mexico" }, new() { Id = 2, CountryName = "mexico" } ];
Human human1 = new() { Id = 1, FirstName = "Karen", LastName = "Payne", BirthDay = new DateOnly(1967,11,1), Country = countries[0] };
Human human2 = new() { Id = 2, FirstName = "Karen", LastName = "Payne", BirthDay = new DateOnly(1968, 11, 1), Country = countries[1] };
CompareLogic compare = new();
compare.Config.MembersToIgnore.Add("*Id");
ComparisonResult result = compare.Compare(human1, human2);
if (result.AreEqual)
{
Debug.WriteLine("Equal");
}
else
{
var json = JsonSerializer.Serialize(result.Differences, new JsonSerializerOptions { WriteIndented = true });
Debug.WriteLine(json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment