2012-12-07 221 views
0

我創建了一個帶有控件的dll。當我瀏覽dll時,它將控件成功添加到工具箱中。問題是,當我運行的應用程序時,我收到以下錯誤: An unhandled exception of type 'System.StackOverflowException' occurred in xxx.dll類型'System.StackOverflowException'未處理的異常

在調試器去突出錯誤是在下面的函數的方法:

public ItemType this[int i] 
{ 
    get 
{ 
    return (ItemType)this[i]; 
} 
set 
{ 
    this[i] = value; 
} 
} 

,因爲我知道發生此錯誤由於遞歸調用,我如何重寫上述或修改它來克服這個問題。請儘快提供任何代碼幫助

謝謝

回答

2

您應該在班級中使用內部列表。

private IList<ItemType> _list = new List<ItemType>(); 
    public ItemType this[int i] 
    { 
     get 
     { 
      return _list[i]; 
     } 
     set 
     { 
      _list[i] = value; 
     } 
    } 
+0

現在我得到的回報_list不設置到對象的實例的對象引用[I]; – user1885308

+0

只需首先初始化列表。當然,在示例代碼中_list始終爲空。 – Tobias

+0

其實itemtype是BaseItemCollection ._ knownTypes = new Type [] {typeof(ItemType)};那麼我應該如何改變遞歸函數 – user1885308

0

我解決了這個問題,具體如下:

public ItemType this[int i] 
{ 
    get 
    { 
     return (ItemType)((IList)this)[i]; 
    } 
    set 
    { 
     this[i] = value; 
    } 
} 
相關問題