Is there any way to remove unused types/code from a project.
lets say I'm using NAudio
(source code) in my console application and I'm only using the WaveIn
class from it. Is there any way for me to remove unused classes from the code and only keep the WaveIn
class and the classes WaveIn
depends upon? Something down the line of tree shaking
Asked
Active
Viewed 587 times
4

Abdullah Saleem
- 3,701
- 1
- 19
- 30
-
You mean remove it from a compiled assembly? Not without recompiling the assembly. – Lasse V. Karlsen Aug 08 '16 at 06:44
-
1Why do you feel a need to do this, what problem are you hoping to solve? – Lasse V. Karlsen Aug 08 '16 at 06:44
-
No I mean from the source code – Abdullah Saleem Aug 08 '16 at 06:44
-
I need to reduce the size of the compiled exe – Abdullah Saleem Aug 08 '16 at 06:45
-
1Sorry, I didn't read your question well enough, my bad, early monday not enough coffee. I don't know of any such tool other than *perhaps* ReSharper which will flag a class as unused but it may not actually give you any help here since presumably there are many classes that uses each other, it's just that *your program* does not use them. – Lasse V. Karlsen Aug 08 '16 at 06:47
-
yes that is the exact problem I'm having – Abdullah Saleem Aug 08 '16 at 06:52
-
Possible duplicate of [Is there a tool for finding unreferenced functions (dead, obsolete code) in a C# app?](http://stackoverflow.com/questions/65585/is-there-a-tool-for-finding-unreferenced-functions-dead-obsolete-code-in-a-c) – Murray Foxcroft Aug 08 '16 at 07:22
1 Answers
4
Ndepend was the answer
from t in Types
let depth0 = t.DepthOfIsUsedBy("NAudioTrim.Program")
where depth0 >= 0 orderby depth0
select new { t, depth0 }
where NAudioTrim.Program
contains the entry point of my application and contains the used types.
Once you get the list of all used types and the types they use you can delete the rest of the files/types

Abdullah Saleem
- 3,701
- 1
- 19
- 30
-
What is Types in this case? You you map from Type to IUsed interface required by DepthOfIsUsedBy method – Vlado Pandžić Feb 08 '23 at 13:27