【问题标题】:access IEnumerable<Type>.GetEnumerator() from IEnumerable<Type>.GetEnumerator()从 IEnumerable<Type>.GetEnumerator() 访问 IEnumerable<Type>.GetEnumerator()
【发布时间】:2010-12-19 10:34:37
【问题描述】:

请查找代码中没有在类中定义getenuemrator()方法的错误

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;
    }
  }
}

【问题讨论】:

  • @digEmAll,这是反编译代码...编译器不介意 gotos ;)

标签: c# generics interface ienumerable explicit-implementation


【解决方案1】:

好吧,既然您要显式实现 两个 GetEnumerator 方法,您可以这样做:

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

但我有两个问题:

  1. 为什么要显式实现两个接口?这根本不是惯用语。 (编辑:现在很清楚为什么;这是生成的代码)。
  2. 此代码甚至是由人类编写的吗?看起来很像 C# 编译器为迭代器块生成的内容。如果是这样,你是如何得到它的?反编译器?请注意,反编译器在正确 IL 上出错并生成无法编译的 C# 代码是很常见的。

编辑:我查看了带有 Reflector 的迭代器块的反编译代码(我怀疑这是您正在使用的反编译器?)。它似乎确实证明了这个错误,即非通用版本反编译为明显无效:

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

如果您能更好地解释您真正想要做什么,我们可以为您提供更好的帮助。修复不正确的反编译代码并不好玩。例如,如果您需要为类型层次结构编写一个枚举器,那么使用迭代器块会更容易很多,而不是去反编译 Dynamic LINQ。

【讨论】:

  • 谢谢...是的,这是反编译的代码。我已经尝试使用您发布的相同代码,但它不起作用。
  • @user547621:在我的机器上,经过其他 2 次修复后,它可以正常编译。当然,您还需要适当的using 指令: using System;使用 System.Diagnostics;使用 System.Collections.Generic;使用 System.Threading;使用 System.Collections;)。
  • 当然它必须嵌套在另一个类中,就我而言你不能拥有private顶级type知道。
  • 错误 3 'System.Linq.Dynamic.ExpressionParser.SelfAndBaseClasses' 没有实现接口成员 'System.Collections.IEnumerator.MoveNext()'。 'System.Linq.Dynamic.ExpressionParser.SelfAndBaseClasses.MoveNext()' 无法实现接口成员,因为它不是公共的。
  • @saurav:我建议您在本网站上发布另一个问题,并附上您的查询。试着清楚你想做什么以及最终目标是什么。干杯,祝你好运!
猜你喜欢
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多