2010-02-18 51 views
0

我是一個典型的新手...無效的轉換異常,同時使用LINQ

我可以編譯我的程序,但在運行PROGRAMM

有什麼錯我的代碼時,你得到的invalidcast錯誤時拋出。我不沒有什麼SEACH爲:(

這是我的代碼(.NET 3.5)

class Element{ 
    private int i; 
    public int I { get { return i; } set { i = value; } } 
    private string s; 
    public string S { get { return s; } set { s = value; } } 

    public Element(string _s, int _i) { 
     this.s = _s; 
     this.i = _i; 
    } 
} 
class myDict : Dictionary<string, Element> { 
    public void afunction() { 

    } 
} 

class Program { 
    static void Main(string[] args) { 

     myDict myDict = new myDict(); 
     myDict.Add("a", new Element("x", 23)); 
     myDict.Add("b", new Element("y", 48)); 

     var sortedDict = ((from entry in myDict orderby entry.Key descending select entry).Take(10)); 

     myDict = (myDict)sortedDict.ToDictionary(v => v.Key, v => v.Value); 
     foreach (KeyValuePair<string, Element> kvp in myDict) { 
      System.Console.WriteLine("-> " +kvp.Key + " " + kvp.Value); 
     } 
     Console.ReadLine(); 
    } 
} 

謝謝!邁克爾:)

+3

不要繼承BCL集合類,例如'List '和'Dictionary '。 –

回答

2

我認爲這個問題是,你鑄造sortedDict.ToDictionary()作爲myDict的結果。

myDict = (myDict)sortedDict.ToDictionary(v => v.Key, v => v.Value); 
0

ToDictionary()不返回myDict類,它返回一個Dictionary類。你不能從Dictionary投到myDict,雖然因爲myDict派生自Dictionary,你可以用其他方式施放。

2

你不能做到這一點:

myDict = (myDict)sortedDict.ToDictionary(v => v.Key, v => v.Value); 

當你調用ToDictionary,您創建一個Dictionary<string, Element>,而不是一個myDict實例。該演員表無效。

但是,沒有理由(在這種情況下)這樣做。您可以直接使用sortedDict結果。

如果你想這樣做,我建議增加一個構造函數myDict需要一個IEnumerable<KeyValuePair<string,Element>>。然後,你可以這樣做:

class myDict : Dictionary<string, Element> { 
    public myDict() {} 
    public myDict(IEnumerable<KeyValuePair<string, Element>> pairs) 
    { 
      foreach(var pair in pairs) 
       this.Add(pair.Key, pair.Value); // Add in all elements 
    } 
} 

然後,而不是演員,你可以這樣做:

myDict myDict = new myDict(); 
myDict.Add("a", new Element("x", 23)); 
myDict.Add("b", new Element("y", 48)); 

var sortedDict = ((from entry in myDict orderby entry.Key descending select entry).Take(10)); 

myDict = new myDict(sortedDict); 

話雖這麼說,這是一個壞主意,直接繼承Dictionary<TKey,TValue>。如果你想製作一個自定義字典,你應該在你自己的類中封裝框架字典,並改爲實現IDictionary<TKey,TValue>

0

您的問題是,因爲它是一個更具體的類比字典

2

的ToDictionary調用返回Dictionary<string, Element>你不能強制轉換回myDict。僅僅因爲myDict是Dictionary<string, Element>並不意味着Dictionary<string, Element>是myDict。

6

你好

您好。

我是一個經典的新手。

歡迎登機。

運行程序

轉換運算符常常意味着「我說的是編譯器,這種轉換是有效的,當我可以編譯我的程序,但得到一個無效轉換異常。如果我犯了一個錯誤,在運行時拋出無效的強制轉換異常。「

我的代碼有什麼問題。我不知道要尋找什麼。

那麼,而不是交給你一條魚 - 字典上的演員是無效的 - 讓我們教你如何捕捉自己的魚。

以調試模式編譯您的程序並在調試器中運行它。調試器應該在異常的確切位置中斷。然後你可以分析那個確切站點的錯誤。

如果由於某種原因,您無法在調試器中運行代碼,請在調試模式下編譯您的程序並正常運行。當出現異常時,請查看異常的「堆棧跟蹤」部分。它應該有異常網站的行號,以便您可以檢查該代碼。

如果由於某些原因您不能這樣做,請使用異常消息來嘗試解決它。 「無效轉換異常」意味着不良行爲是轉換運算符的結果。您的程序中有兩個地方使用了演員操作符。字典中有一個明確的用法,並且在foreach循環中有一個隱含的用法。

很多人都很驚訝地瞭解後者。當你說

foreach(Giraffe giraffe in animals) 

,實際上意味着「劇組的每個動物的動物長頸鹿到列表中,並給出一個無效的轉換異常,如果其中一人是老虎或鼠或東西」。這並不意味着「忽略所有不屬於長頸鹿動物」 - 如果你想要的,然後說

foreach(Giraffe giraffe in animals.OfType<Giraffe>()) 

問題幾乎肯定會在這兩個地方之一。

+2

+1:我是一個剛學過'foreach(長頸鹿長頸鹿in animals.OfType ())' –