12

I am creating a complex class with AssemblyBuilder that Im later creating objects from. There is however uncertainties in how this class is really contructed. So is there any way to write this dynamicly created class to a cs file for inspection?

I can get the dll file written to disk but I need the cs file.

Banshee
  • 15,376
  • 38
  • 128
  • 219

4 Answers4

5

You can decompile managed .NET dll to C# code using

  • DotPeek by JetBrains (free, sources are closed)
  • ILSpy open source project (MIT license sources are available at github)
  • Reflector by Red Gates (Paid tool, sources are closed)
  • JustDecompile by Telerik (free with open source decompilation engine available at github Apache License)
  • There is also a Microsoft's ildasm tool.

If you need to write custom tool you can download open-source code and give it a try.

Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • From what I read a decompile file will probably not look exacly like the srouce? – Banshee Sep 19 '18 at 06:42
  • @Banshee it depends on tool. Some of them produce normal source code which can be compiled back. – Access Denied Sep 19 '18 at 06:49
  • 1
    one should also mention [dnSpy](https://github.com/0xd4d/dnSpy) which is using the ILSpy decompiler but is noticable more feature rich – X39 Sep 20 '18 at 13:41
1

Do you have a requirement to use AssemblyBuilder? I'm asking because AssemblyBuilder wont allow you to see the generated class without using a decompiler and if the class you´re generating is quite complicated, the decompiled code wont be of good quality.
You are almost in the same situation if you use Reflection.Emit because it generates low level IL.
If you really need to see the source code that you're generating dynamically your best option is CodeDom. Here's an example How to: Create a Class Using CodeDOM

emmanuel
  • 765
  • 8
  • 15
  • I have used CodeDOM in the past with success. Even with non-optimal placing of using statements(inside of the class?/Namespace?,I cannot remember which as opposed to the file level), the end product was exceptional. –  Sep 22 '18 at 02:59
0

You might be able to kill two bird with one stone with Roslyn (aka ".NET Compiler Platform"). You'll need the package Microsoft.CodeAnalysis.CSharp.

First, you can use the SyntaxFactory class to generate syntax nodes, which you can combine into larger structures (members, methods, classes, namespaces, compilation units).
You can also get a nicely formatted representation of your syntax nodes with ToString() or ToFullString() (with correct indentation and line breaks and everything), which is what you were originally looking for.

There are quite a few tutorials online on how to use this API (like 1, 2), and there's the Roslyn Quoter website that can convert a piece of C# code into SyntaxFactory calls.

Second, you can then use the resulting CSharpSyntaxNode to create a CSharpSyntaxTree, which you can compile into IL with the help of CSharpCompilation (after all, Roslyn is the reference C# compiler).
If you want, you can even emit the generates assembly into a stream, get the assembly's binary data from there, and load your newly created assembly into your currently executing assembly, and dynamically instantiate the types you just defined.

jan.h
  • 524
  • 5
  • 11
0

You need to use the .NET reflection.

Ildasm.exe cannot help you because it will not create the .cs file you need.

So either the ILSpy is the open-source .NET assembly browser and decompiler from the SharpDevelop team or dotPeek from Jetbrains.

Depending on the platform you may also check Mono Cecil. Cecil is a library written by Jb Evain to generate and inspect programs and libraries in the ECMA CIL format.

If you need speed JustDecompile from Telerik is a free tool for .NET assembly browsing and decompiling that claims to be 10x faster than competitors.

All these tools lets you take an existing compiled assembly (.dll or .exe) and easily browse the symbols it contains, and then just as easily decompile the assembly language back to readable C# and IL.

prosti
  • 42,291
  • 14
  • 186
  • 151