创建访问器列表将抛出ArgumentException

本文关键字:ArgumentException 列表 访问 创建 | 更新日期: 2023-09-27 18:17:56

当创建访问器列表时,我得到一个非常奇怪的错误。
我有一个类ComponentComponent s的私有列表。因为我想测试一些类的行为关于这个列表,我使用Component类的访问器。此外,我在类的构造函数中有一些依赖关系,我想要避免。因此,我在TestInitialize() -Method

中手动实例化列表TestInitialize()代码如下所示:
private string _testIdentifier = "TestComponent";
private int _testConsist = 1;
private Component_Accessor _target;
[TestInitialize()]
    public void MyTestInitialize()
    {
        _target = new Component_Accessor();
        _target.Identifier = new ComponentIdentifier(_testIdentifier, _testConsist);
        _target._inputComponents = new List<Component_Accessor>();
        _target._outputComponents = new List<Component_Accessor>();
    }

访问器代码如下所示:

[Shadowing("_inputComponents")]  
public List<Component_Accessor> _inputComponents { get; set; }

这个编译得很好,但是我得到一个RunTimeException:

类型为"System.Collections.Generic.List"1的对象[DT5_Training_Simulator.Model.Components]。不能转换为类型"System.Collections.Generic.List"1[DT5_Training_Simulator.Model.Components. list]。组件)"

实际上我很茫然。谁能告诉我我做错了什么?

创建访问器列表将抛出ArgumentException

您不能对公共成员使用Shadowing属性,因为这会使测试人员感到困惑。据此,Shadowing用于测试私有属性和方法,就好像它们是公共的一样。您的属性已经是公共的,所以您需要从声明中删除Shadowing