Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包含带有谓词的属性时发生异常_C#_.net_Fluent Assertions - Fatal编程技术网

C# 包含带有谓词的属性时发生异常

C# 包含带有谓词的属性时发生异常,c#,.net,fluent-assertions,C#,.net,Fluent Assertions,我试图比较两个具有多个属性的对象,但需要使用谓词比较特定属性(object1在object2中没有这些属性的精确值,因此我需要比较其中的部分字符串) 所以,我试着: object1.Should().BeEquivalentTo(object2, options => options .Including(o => o.Property1.StartsWith("something")) .Including(o => o.Property2.StartsWit

我试图比较两个具有多个属性的对象,但需要使用谓词比较特定属性(
object1
object2
中没有这些属性的精确值,因此我需要比较其中的部分字符串)

所以,我试着:

object1.Should().BeEquivalentTo(object2, options => options
    .Including(o => o.Property1.StartsWith("something"))
    .Including(o => o.Property2.StartsWith("something else")
);
我希望所有其他属性都能像往常一样进行比较

但是,运行上述代码会引发异常:

消息:System.ArgumentException:表达式
不能用于选择成员。 参数名称:表达式

我查看了文档,它的示例与我的示例相同(第页的“选择成员”一章)

为什么会发生此异常?如何修复它

为什么会出现这种异常

发生异常是因为您正在调用函数

.Including(o => o.Property1.StartsWith("something")) //<-- expects property only
为什么会出现这种异常

发生异常是因为您正在调用函数

.Including(o => o.Property1.StartsWith("something")) //<-- expects property only

@你说的“更好的例子”是什么意思?我有两个具有一组字符串属性的相同类型的对象。我想断言这些对象是相等的,但对于某些特定属性,断言应该基于谓词(.StartsWith())进行。断言是
object1.Property1.StartsWith(“https://www.google.com“”
equals
object2.Property1=”https://www.google.com/search?q=test“
@Nkosi“更好的例子”是什么意思?”? 我有两个具有一组字符串属性的相同类型的对象。我想断言这些对象是相等的,但对于某些特定属性,断言应该基于谓词(.StartsWith())进行。断言是
object1.Property1.StartsWith(“https://www.google.com“”
equals
object2.Property1=”https://www.google.com/search?q=test“
[TestClass]
public class ObjectEquivalencyTests {
    [TestMethod]
    public void ShouldBeEquivalent() {

        var expected = new MyObject {
            Property1 = "https://www.google.com",
            Property2 = "something else"
        };

        var actual = new MyObject {
            Property1 = "https://www.google.com/search?q=test",
            Property2 = "something else"
        };

        actual.Should().BeEquivalentTo(expected, options => options
            .Using<string>(ctx => ctx.Subject.Should().StartWith(ctx.Expectation))
            .When(info => info.SelectedMemberPath == "Property1")
        );
    }
}

public class MyObject {
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}