1

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?

Sören Titze
  • 985
  • 1
  • 12
  • 31
  • 1
    Your code doesn't match your description - you're creating a new `Component_Accessor`, not a new `Component`. Are you using dynamic typing at all? If you could post a more complete section of code, that would really help. – Jon Skeet Dec 13 '11 at 13:52
  • can you provide more code showing the use of `Component` class also. probably the exception isnot becaused of provided code. – Azodious Dec 13 '11 at 13:55
  • is dont know what your ShadowingAttribute really does but MSDN says: "Do not use this class" --> possibly you shouldnt? – fixagon Dec 13 '11 at 13:57
  • I provided the complete initialization code. Also when I debugged the code, the Exception was raised in line 11 of the excerpt (the part with `_target._inputComponents = new List();`) – Sören Titze Dec 13 '11 at 14:33
  • I'd suggest a clean in visual studio. Also what does that Shadowing annotation do? I can't find anything about it. – Sign Dec 13 '11 at 14:41
  • I cleaned and rebuild the whole project several times. And Shadowing provides me (in this case) with a mean to access private properties of a class. A good and short description can be found here: [StackOverflow Shadowing](http://stackoverflow.com/questions/673779/what-is-shadowing) – Sören Titze Dec 13 '11 at 14:52

1 Answers1

0

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.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523