2011-12-13 36 views
1

創建訪問者列表時,我收到一個非常奇怪的錯誤。
我有一個類Component私人清單Component s。因爲我想測試一些關於這個列表的類行爲,所以我使用了一個Component類的訪問器。另外我在我想避免的類的構造函數中有一些依賴項。因此,我手動在TestInitialize() - 方法創建訪問者列表拋出一個ArgumentException

的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.Li st'1 [DT5_Training_Simulator.Model.Components.Component_Accessor]」不能被轉換爲類型 「System.Collections.Generic.List'1 [DT5_Training_Simulator.Model.Components.Component]」

其實我在這裏相當茫然。有誰能告訴我我做錯了什麼嗎?

+1

您的代碼與您的描述不符 - 您正在創建一個新的「Component_Accessor」,而不是新的「Component」。你是否使用動態打字?如果您可以發佈更完整的代碼部分,那真的很有幫助。 –

+0

你能否提供更多的代碼展示'Component'類的用法。可能這個例外不是因爲提供了代碼。 – Azodious

+0

是不知道你的ShadowingAttribute真的做了什麼,但MSDN說:「不要使用這個類」 - >可能你不應該? – fixagon

回答

0

您不得在公共成員中使用Shadowing屬性,因爲它會混淆測試人員。根據thisShadowing用於測試私有屬性和方法,就好像它們是公共的。您的財產已經公開,因此您需要從其聲明中刪除Shadowing

相關問題