.net Reflector decompiled C# code won't compil

2020-02-07 07:17发布

问题:

I tried to decompile a C# console application and compile it again in Visual C# 2010, but there are many errors in the code. Here is an example:

 public static Test mTest
    {
        [CompilerGenerated]
        get
        {
            return <mTest>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            <mTest>k__BackingField = value;
        }
    }

I've set the .net framework version to 3.5 in .net Reflector. Is there any way to get code that is able to recompile from .net Reflector?

回答1:

There is no straight way to overcome this limitation. Compiler created IL from your source code and this IL may not contain information regarding your initial source code. For example when you write

public string Property { get; set; }

Compiler creates backing field (for example <Property >k__BackingField ) and names it using special symbols, that you can't use to name your field in the source code. You gave the example above, where reflector tried to deduce what compiler meant.

I've used dotPeek (free decompiler by JetBrains) and it understand autoproperties, so you would see correct code in your example. But again - there might be cases, where dotPeek won't be able to get the initial source code.