C#将类成员与预定义的类/变量类型进行比较

时间:2016-06-05 19:42:28

标签: c# class inheritance compare comparison

我想知道将类成员(不是所有成员)与某个预定义对象进行比较的最佳方法是什么。

所以例如

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

所以它可以这样使用:

    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

请注意,它应采用这种格式,可能使用" =="操作员或接近那种风格的东西。

1 个答案:

答案 0 :(得分:1)

我会用fluentassertions这很酷。那么下面的代码是可能的:

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")); 

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