Joe
Joe

Reputation: 721

remove unused preprocessor symbols in c# binary

I have a project with a few conditional builds. I compiled it. And then used dotpeek to look at it. I noticed ALL conditional statements and the code that it wraps even when not defined.

Since this app is modified in some key ways for different clients, I would like a build for that client have a binary that ONLY contains the active #ifs that have been defined.

How can I do an automated build that achieves this?

Upvotes: 1

Views: 384

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59303

dotPeek does 2 things

  1. it can decompile IL code
  2. it can read the PDB file and look up the source file

You can see this in the context menu. One entry is "Decompiled sources" and the other is "Sources from symbol files":

Context menu

By double clicking an item, it will first try to perform action 2, which is: show the source file that exists on your hard disk.

Here you see a very simple program:

  • left: Sources from symbol files
  • middle: Decompiled sources
  • right: IL code

As you can see, the compiled code only contains those parts which it was compiled for.

Comparison

Therefore, there's no need to strip those parts in a build script, because the DLL or EXE does not contain it.

Upvotes: 3

Related Questions