创建访问器列表会引发 ArgumentException
创建访问器列表时出现一个非常奇怪的错误。
我有一个 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 Component
s. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将
Shadowing
属性与公共成员一起使用,因为它会使测试人员感到困惑。根据这个< /a>,Shadowing
用于测试私有属性和方法,就好像它们是公共的一样。您的属性已经是公共的,因此您需要从其声明中删除Shadowing
。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 removeShadowing
from its declaration.