创建访问器列表会引发 ArgumentException

发布于 2024-12-21 11:07:48 字数 1253 浏览 0 评论 0原文

创建访问器列表时出现一个非常奇怪的错误。
我有一个 Component 类,其中包含一个 Component 的私有列表。由于我想测试有关此列表的一些类行为,因此我使用 Component 类的访问器。另外,我想避免的类的构造函数中有一些依赖项。因此,我在 TestInitialize()-方法 中手动实例化列表

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.Component_Accessor]”的对象无法转换为类型“System.Collections.Generic.List'1[DT5_Training_Simulator.Model”。组件.组件]"

其实我在这里很茫然。有人可以告诉我我做错了什么吗?

I get a very odd Error when creating a List of Accessors.
I have a class Component with a private List of Components. Since I want to test some of the classes behaviour regarding this list I use an Accessor of the Component-class. Also I have some dependencies in the constructor of the class I want to avoid. Therefore I manually instantiate the list in a TestInitialize()-Method

The TestInitialize()-Code looks like this:

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>();
    }

The Accessor-Code looks like this:

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

This compiles just fine but I get a RunTimeException:

The Object of the type "System.Collections.Generic.List'1[DT5_Training_Simulator.Model.Components.Component_Accessor]" can not be converted to the type "System.Collections.Generic.List'1[DT5_Training_Simulator.Model.Components.Component]"

Actually I'm quite at a loss here. Could anybody please tell me what I did wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-12-28 11:07:48

You may not use Shadowing attribute with public members, because it confuses the tester. According to this, Shadowing is used to test private properties and methods as if they were public. Your property is already public, so you need to remove Shadowing from its declaration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文