简体   繁体   English

通过DecompileTypeAsString反编译时如何反编译生成相同代码的特定方法

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

NOOB with ILSPY菜鸟与 ILSPY

I'm trying to decompile a method via ILSpy.我正在尝试通过 ILSpy 反编译一个方法。 When I use the following, I get the code re-generated that is pretty close to what was typed in the original C#当我使用以下内容时,我重新生成了与原始 C# 中键入的代码非常接近的代码

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

The generated code looks something like follows.生成的代码如下所示。

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

Whereas when I use the following snippet,而当我使用以下代码段时,

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

The code generated is as following:生成的代码如下:

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

Is there a way to generate the code as generated by DecompileTypeAsString by decompiling only one function?有没有办法通过只反编译一个函数来生成由 DecompileTypeAsString 生成的代码?

I filed an issue on ILSpy and it has been marked as enhancement.我在 ILSpy 上提交了一个问题,它已被标记为增强。 So guess it isn't possible at the moment.所以猜测目前是不可能的。

I'll go the ugly regex route for now and check back in on the enhancement in later versions.我现在会走丑陋的正则表达式路线,并在以后的版本中检查增强功能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM