Skip to content

Ignoring Members

Greg Finzer edited this page Sep 12, 2020 · 4 revisions

Compare .NET Objects has the ability to ignore Properties and Fields of classes in these ways.

Ignore By Expression

CompareLogic compare = new CompareLogic();
compare.Config.IgnoreProperty<Person>(x => x.Name);

Ignore By the ClassName.MemberName

CompareLogic compare = new CompareLogic();
compare.Config.MembersToIgnore.Add("Person.Name");

Ignore All Members with a Name

CompareLogic compare = new CompareLogic();
compare.Config.MembersToIgnore.Add("UpdateDate");

Ignore with a Wildcard

CompareLogic compare = new CompareLogic();
compare.Config.MembersToIgnore.Add("*Id");

Ignore with an Attribute

[AttributeUsage(AttributeTargets.Property)]
public sealed class CompareIgnoreAttribute : Attribute
{
}

public class Shipment
{
	public long IdentCode { get; set; }
	public String Customer { get; set; }
	
	[CompareIgnore]
	public DateTime InsertDate { get; set; }
}

CompareLogic compare = new CompareLogic();
compare.Config.AttributesToIgnore.Add(typeof(CompareIgnoreAttribute));

Compare Only Properties with an Attribute

public class Movie 
{
	[Compare]
	public string Name { get; set; }

	public decimal PaymentForTomCruise { get; set; }
}

CompareLogic compare = new CompareLogic();
compare.Config.RequiredAttributesToCompare.Add(typeof(CompareAttribute));