2010-12-19 53 views
1

請找代碼有錯誤getenuemrator()方法是在類訪問的IEnumerable <Type> .GetEnumerator()

private sealed class SelfAndBaseClasses : IEnumerable<Type>, IEnumerator<Type>, IEnumerable, IEnumerator, IDisposable 
{ 
    private int state; 
    private Type current; 
    public Type type; 
    private int initialThreadId; 
    //public Type type; 

    [DebuggerHidden] 
    public SelfAndBaseClasses(int state) 
    { 
    this.state = state; 
    this.initialThreadId = Thread.CurrentThread.ManagedThreadId; 
    } 

    private bool MoveNext() 
    { 
    switch (this.state) 
    { 
     case 0: 
     this.state = -1; 
     break; 

     case 1: 
     this.state = -1; 
     this.type = this.type.BaseType; 
     break; 

     default: 
     goto Label_0055; 
    } 
    if (this.type != null) 
    { 
     this.current = this.type; 
     this.state = 1; 
     return true; 
    } 
    Label_0055: 
    return false; 
    } 

    [DebuggerHidden] 
    IEnumerator<Type> IEnumerable<Type>.GetEnumerator() 
    { 
    ExpressionParser.SelfAndBaseClasses d; 
    if ((Thread.CurrentThread.ManagedThreadId == this.initialThreadId) && (this.state == -2)) 
    { 
     this.state = 0; 
     d = this; 
    } 
    else 
    { 
     d = new ExpressionParser.SelfAndBaseClasses(0); 
    } 
    d.type = this.type; 
    return d; 
    } 

    [DebuggerHidden] 
    IEnumerator IEnumerable.GetEnumerator() 
    { 
    return this.System.Collections.Generic.IEnumerable<System.Type>.GetEnumerator(); 
    } 

    [DebuggerHidden] 
    void IEnumerator.Reset() 
    { 
    throw new NotSupportedException(); 
    } 

    void IDisposable.Dispose() 
    { 
    } 

    Type IEnumerator<Type>.Current 
    { 
    [DebuggerHidden] 
    get 
    { 
     return this.current; 
    } 
    } 

    object IEnumerator.Current 
    { 
    [DebuggerHidden] 
    get 
    { 
     return this.current; 
    } 
    } 
} 
+2

哇一個GOTO! :D – digEmAll 2010-12-19 10:46:23

+1

@digEmAll,這是反編譯的代碼...編譯器不介意gotos;) – 2010-12-19 10:58:39

回答

2

明確界定,因爲你正在實施GetEnumerator方法明確,你可以這樣做:

IEnumerator IEnumerable.GetEnumerator() 
{ 
    return ((IEnumerable<Type>)this).GetEnumerator(); 
} 

但我有兩個問題:

  1. 爲什麼你希望明確實施這兩個的接口?這根本不是慣用的。 (編輯:現在清楚爲什麼;這是生成的代碼)。
  2. 這段代碼是否是由人寫的?看起來很像C#編譯器爲迭代器塊生成的內容。如果是這樣,你是如何得到它的?反編譯器?請注意,反編譯器通常會在正確的 IL中被絆倒,並生成無法編譯的C#代碼。

編輯:我接過來一看,在與反射迭代器塊的反編譯的代碼(我懷疑這是您正在使用的反編譯器?)。它似乎證實了這個bug,即非通用版本的反編譯爲明顯無效:

return this.System.Collections.Generic.IEnumerable<Foo>.GetEnumerator(); 

編輯: 其他修復你需要得到這個編譯似乎是:

  1. MoveNext方法的可訪問性更改爲public
  2. 通用GetEnumerator方法中刪除提及ExpressionParser.的提及。

,您可以嘗試在Ideone

foreach (Type t in new SelfAndBaseClasses(0) { type = typeof(string) }) 
    Console.WriteLine(t); 

輸出:

System.String 
System.Object 

如果你能更好地解釋你真的想要做什麼,我們可以幫助你更好。修復錯誤地反編譯的代碼並不有趣。例如,如果你需要爲一個類型層次結構編寫一個枚舉器,這將是很多更容易做一個迭代器塊,而不是去反編譯動態LINQ的麻煩。

+0

謝謝...是的,這是反編譯的代碼。並且我嘗試了與您發佈的相同的代碼,但它不起作用。 – saurav 2010-12-19 10:54:27

+0

@ user547621,它給了什麼錯誤? – 2010-12-19 10:57:55

+0

@ user547621:在我的機器上,它在2個其他修復之後編譯正常。當然,你也需要適當的'using'指令:using System; using System.Diagnostics; using System.Collections.Generic;使用System.Threading的 ; using System.Collections;)。 – Ani 2010-12-19 11:13:08

相關問題