2010-12-19 35 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

相关问题