出於好奇,我決定嘗試反編譯我的項目代碼。我拿着Assembly.dll文件並使用ILSpy對其進行反編譯。它似乎工作正常,除了IEnumerator<>
方法。反編譯IEnumerators
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沒有處理<
和>
正確的,但是這是值得的代碼什麼?
這確實看起來像編譯器會產生什麼。 – Enigmativity
說了JetBrains dotPeek似乎在這方面做得更好。 – Enigmativity
我檢查過dotPeek,並且在代碼中禁用顯示標記時,它看起來幾乎與ILSpy中的代碼相同,但隨着啓用它,它變得非常混亂。我在第一篇文章中發佈了代碼。它有任何意義嗎?如果我只是將名稱從「 __ + @!##」更改爲正常的名稱,它會起作用嗎? – Reynevan