簡體   English   中英

通過DecompileTypeAsString反編譯時如何反編譯生成相同代碼的特定方法

[英]How to decompile a specific method generating the same code when decompiled via DecompileTypeAsString

菜鳥與 ILSPY

我正在嘗試通過 ILSpy 反編譯一個方法。 當我使用以下內容時,我重新生成了與原始 C# 中鍵入的代碼非常接近的代碼

decompiler.DecompileTypeAsString(new FullTypeName($@"{MyFullTypeName}"));

生成的代碼如下所示。

public async Task<IReadOnlyList> FuncName(parameterList)
{
//Actual code body
}

而當我使用以下代碼段時,

string functionName = "ReflectionNamed__5";
var name = new FullTypeName(functionName);
ITypeDefinition typeInfo = decompiler.TypeSystem.MainModule.Compilation.FindType(name).GetDefinition();
if (typeInfo.Methods.First().HasBody)
{
    var tokenOfFirstMethod = typeInfo.Methods.First().MetadataToken;
    var methodCodeString = decompiler.DecompileAsString(tokenOfFirstMethod);
    Console.WriteLine(methodCodeString);
}

生成的代碼如下:

//Using statements

    private void MoveNext()
    {
        int num = <>1__state;
        IReadOnlyList<string> result;
        try
        {
                if (num != 0)
                {
                        //Abstraction of a using statement
                }
                try
                {
                        TaskAwaiter<IReadOnlyList<string>> awaiter;
                        if (num != 0)
                        {
                                //Somewhat resembles the actual code
                        }
                        else
                        {
                                awaiter = <>u__1;
                                <>u__1 = default(TaskAwaiter<IReadOnlyList<string>>);
                                num = (<>1__state = -1);
                        }
                        result = awaiter.GetResult();
                }
                finally
                {
                        if (num < 0 && <oLogger>5__2 != null)
                        {
                                <oLogger>5__2.Dispose();
                        }
                }
        }
        catch (Exception exception)
        {
                <>1__state = -2;
                <>t__builder.SetException(exception);
                return;
        }
        <>1__state = -2;
        <>t__builder.SetResult(result);
    }

有沒有辦法通過只反編譯一個函數來生成由 DecompileTypeAsString 生成的代碼?

我在 ILSpy 上提交了一個問題,它已被標記為增強。 所以猜測目前是不可能的。

我現在會走丑陋的正則表達式路線,並在以后的版本中檢查增強功能。

https://github.com/icsharpcode/ILSpy/issues/1935

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM