C# 为什么可以';当抽象类不公开每个构造函数参数时,我是否创建相似性代理?

C# 为什么可以';当抽象类不公开每个构造函数参数时,我是否创建相似性代理?,c#,unit-testing,semantic-comparison,C#,Unit Testing,Semantic Comparison,我使用Ploeh的SemanticComparison库取得了巨大的成功——除非涉及到一个抽象类,它没有公开所有的构造函数参数 这是我得到的一个例外- Ploeh.SemanticComparison.ProxyCreationException : The proxy of Foo could not be created using the same semantic heuristics as the default semantic comparison. In order to cre

我使用Ploeh的SemanticComparison库取得了巨大的成功——除非涉及到一个抽象类,它没有公开所有的构造函数参数

这是我得到的一个例外-

Ploeh.SemanticComparison.ProxyCreationException : The proxy of Foo could not be created using the same semantic heuristics as the default semantic comparison. In order to create proxies of types with non-parameterless constructor the values from the source constructor must be compatible to the parameters of the destination constructor.
  ----> System.InvalidOperationException : Operation is not valid due to the current state of the object.
这是我能想到的最简单的例子-

// this fails with the aforementioned exception
_fixture.Create<Foo>().AsSource().OfLikeness<Foo>().CreateProxy();

public class Foo : Bar
{
    public Foo(int age)
        : base(age)
    {           
    }
}

public abstract class Bar
{
    private readonly int _age;

    protected Bar(int age)
    {
        _age = age;
    }
}
//此操作失败,出现上述异常
_fixture.Create();
公开课Foo:Bar
{
公共食品(国际时代)
:基数(年龄)
{           
}
}
公共抽象类栏
{
私人只读int_age;
受保护条(整数)
{
_年龄=年龄;
}
}
然而,如果我将
public int NotAge{get;set;}
添加到抽象类
Bar
,那么一切都很好。我真的认为这是一个次优的解决方案,因为我不想暴露属性<代码>年龄>代码>。它只是用来计算其他东西


我如何解决这个问题而不只是为了测试而暴露属性。是否有另一个库可以在没有此问题的情况下实现相同的效果?

当获取目标类的属性并匹配到源类型的构造函数时,会出现此错误,尽管该错误看起来好像只映射了构造函数

在您的例子中,内部异常是因为两个类中都没有公共属性。我很确定你的修复只是将映射重定向到你的虚拟属性

您可以在基类上使用
public int age{get{return{u age;}}}
来修复它-在本例中没有什么害处

对于此类问题,通常的转义图案填充是使用
InternalVisibleTo
,但库当前在映射类型时仅使用
BindingFlags.Public
,因此不会看到为此目的创建的内部属性

我能够通过调整使用
BindingFlags.NonPublic
BindingFlags.Public
来创建代理,但我不确定这是一种合理的方法