c# - 反编译 IEnumerators

标签 c# .net unity3d decompiling ilspy

出于好奇,我决定尝试反编译我的项目代码。我获取了 Assembly .dll 文件并使用 ILSpy 对其进行了反编译。它似乎工作正常,除了 IEnumerator<>方法。

IEnumerator sP()
{
        for (int i = 0; i < maxEnemies; i++)
        {
            var p = Porczaks[Random.Range(0, Porczaks.Length)];
            Instantiate(p, new Vector3(Random.Range(245, 360), 16.8f, Random.Range(292, 366)), Quaternion.Euler(0f, Random.Range(0f, 359f), 0f));
            yield return new WaitForEndOfFrame();
        }
}

... 例如被解码为:

[DebuggerHidden]
private IEnumerator sP()
{
    WaveManager.<sP>c__Iterator7 <sP>c__Iterator = new WaveManager.<sP>c__Iterator7();
    <sP>c__Iterator.<>f__this = this;
    return <sP>c__Iterator;
}

有没有办法准确反编译IEnumerator

编辑: 我使用 dotPeek 反编译器反编译了同一个程序集,它创建了更多代码。虽然我仍然不确定变量是否可以在 .net 中使用这样的名称:

// Method sP with token 060000AB
[/*Attribute with token 0C000051*/DebuggerHidden]
private IEnumerator sP()
{
    WaveManager.\u003CsP\u003Ec__Iterator7 sPCIterator7 = new WaveManager.\u003CsP\u003Ec__Iterator7();
    sPCIterator7.\u003C\u003Ef__this = this;
    return (IEnumerator) sPCIterator7;
}

// Type <sP>c__Iterator7 with token 02000031
[/*Attribute with token 0C000026*/CompilerGenerated]
private sealed class \u003CsP\u003Ec__Iterator7 : IEnumerator<object>, IEnumerator, IDisposable
{
    // Field <i>__0 with token 040000C7
    internal int \u003Ci\u003E__0;
    // Field <p>__1 with token 040000C8
    internal GameObject \u003Cp\u003E__1;
    // Field $PC with token 040000C9
    internal int \u0024PC;
    // Field $current with token 040000CA
    internal object \u0024current;
    // Field <>f__this with token 040000CB
    internal WaveManager \u003C\u003Ef__this;

    // Property System.Collections.Generic.IEnumerator<object>.Current with token 17000017
    object IEnumerator<object>.System\u002ECollections\u002EGeneric\u002EIEnumerator\u003Cobject\u003E\u002ECurrent
    {
      // Method System.Collections.Generic.IEnumerator<object>.get_Current with token 060000EA
      [/*Attribute with token 0C00006E*/DebuggerHidden] get
      {
        return this.\u0024current;
      }
    }

    // Property System.Collections.IEnumerator.Current with token 17000018
    object IEnumerator.Current
    {
      // Method System.Collections.IEnumerator.get_Current with token 060000EB
      [/*Attribute with token 0C00006F*/DebuggerHidden] get
      {
        return this.\u0024current;
      }
    }

    // Method .ctor with token 060000E9
    public \u003CsP\u003Ec__Iterator7()
    {
      base.\u002Ector();
    }

    // Method MoveNext with token 060000EC
    public bool MoveNext()
    {
      uint num = (uint) this.\u0024PC;
      this.\u0024PC = -1;
      switch (num)
      {
        case 0:
          this.\u003Ci\u003E__0 = 0;
          break;
        case 1:
          this.\u003Ci\u003E__0 = this.\u003Ci\u003E__0 + 1;
          break;
        default:
          return false;
      }
      if (this.\u003Ci\u003E__0 < this.\u003C\u003Ef__this.maxEnemies)
      {
        this.\u003Cp\u003E__1 = this.\u003C\u003Ef__this.Porczaks[UnityEngine.Random.Range(0, this.\u003C\u003Ef__this.Porczaks.Length)];
        UnityEngine.Object.Instantiate((UnityEngine.Object) this.\u003Cp\u003E__1, new Vector3((float) UnityEngine.Random.Range(245, 360), 16.8f, (float) UnityEngine.Random.Range(292, 366)), Quaternion.Euler(0.0f, UnityEngine.Random.Range(0.0f, 359f), 0.0f));
        this.\u0024current = (object) new WaitForEndOfFrame();
        this.\u0024PC = 1;
        return true;
      }
      this.\u0024PC = -1;
      goto default;
    }

    // Method Dispose with token 060000ED
    [/*Attribute with token 0C000070*/DebuggerHidden]
    public void Dispose()
    {
      this.\u0024PC = -1;
    }

    // Method Reset with token 060000EE
    [/*Attribute with token 0C000071*/DebuggerHidden]
    public void Reset()
    {
      throw new NotSupportedException();
    }
}

似乎 dotPeek 没有处理 <>正确,但是这段代码有值(value)吗?

最佳答案

您确实看到了编译器生成的用于替换 yield return 语句的样板代码。是的,它确实是一个状态机。

通常反编译器应该能够识别编译器生成的样板代码并将其替换为正确的 C# 语句。然而,这种识别是通过模式匹配完成的,即样板代码应以非常特定的方式构建。如果编译器生成等效代码但结构不同(例如,由于编译器升级、优化等),则反编译器无法匹配模式并识别 yield 语句。

您应该做的是向反编译器团队提交错误报告,以便修复此问题,而您不必手动重命名内容。您是否在该程序集上尝试过 JustDecompile?也会失败吗?如果是这样,您可以将其发布在 Telerik 论坛中,我们会处理。

附带说明一下,您使用的是什么编译器?

关于c# - 反编译 IEnumerators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38733346/

相关文章:

c# - 使用变量来引用局部 View 的位置

c# - 涉及泛型和自继承的继承混淆

javascript - 如何在启用 Windows 身份验证的情况下发送 API 请求?

c# - 为什么不能从 backgroundworker 访问 UI 组件?

c# - Unity 2D - Sprite 在跟随物体时闪烁

c# - EventSystem OnPointerXXX 函数未被调用

c# - 如何在静态函数中选择<T>

c# - 如果我不能在 C# 异步编程中使用 TLS,我可以使用什么?

c# - Entity Framework 6从存储过程获取复杂的返回值

c# - 带有(一点点)Unity 3D 的 iOS 项目?