ex0ff
ex0ff

Reputation: 25

C# Comparing Class members to a predefined class/variable type

I want to know what's the best way to compare class members (not all members) to some predefined object.

So for example

public class myClass
{
    int A { get; set; }
    int B { get; set; }
    int C { get; set; }
    int D { get; set; }
}

So it can be used like that:

    myClass testClass = new myClass();
    testClass.A = 1;
    testClass.B = 2;
    testClass.C = 3;

   testClass == predefinedObject -> true when A = 1, B = 2, C = 3;
    or 
   testClass == predefinedObject2 -> true when A = 4, B = 5, C = 6;
    etc

Please keep in mind that it should be in this format maybe using the "==" operator or something close to that style.

Upvotes: 0

Views: 56

Answers (1)

wake-0
wake-0

Reputation: 3968

I would use fluentassertions this is very cool. then the following code is possible:

orderDto.ShouldBeEquivalentTo(order, options => 
options.ExcludingMissingMembers());

orderDto.ShouldBeEquivalentTo(order, options => 
options.Excluding(o => o.Customer.Name));

orderDto.ShouldBeEquivalentTo(order, options => options 
.Excluding(ctx => ctx.SelectedMemberPath == "Level.Level.Text")); 

or

orderDto.ShouldBeEquivalentTo(order, options => options
.Including(o => o.OrderNumber)
.Including(pi => pi.PropertyPath.EndsWidth("Date")); 

Upvotes: 1

Related Questions