5

I am trying to find out all the classes and assemblies which are using/referencing a particular class. Is there any way to find out through programming in C#?

For example, if Class1 is called/referenced by Class2 in Assembly1, Class3 in Assembly2, I want to find Class2 & Class3 using C# programming.

Is there anyone who could help me? Is there any tool that could help in this? I want to write a small program to read class from one assembly and find the classes where that particular class is referenced/used.

My question in short: How to find unused class or method in my codebase by writing a C# program as a part of code cleanup activity?

ebb
  • 9,297
  • 18
  • 72
  • 123
nRk
  • 1,251
  • 7
  • 24
  • 50
  • 3
    Is it in compile-time or run-time? – oleksii Dec 30 '11 at 12:29
  • "This tag is for assembly language programming questions not specific to any one processor." -- please use the [.net-assembly] tag instead – Stefan Paul Noack Dec 30 '11 at 12:36
  • If runtime, it is possible through reflection, but you will have to enumerate through all assemblies, all their classes, all methods, properties and constructors and their parameters etc. of the classes and then you will still not catch references only used within the code body of the methods etc. – Casperah Dec 30 '11 at 12:38
  • Do you need to find the assmembly that only exists on one machine which was used to do a bit of trivial coding 6 months ago, and has since been switched off? Or just within the current AppDomain? – Damien_The_Unbeliever Dec 30 '11 at 12:39
  • now the question is a duplicate: http://stackoverflow.com/questions/245963/find-unused-code – Stefan Paul Noack Dec 30 '11 at 13:17

1 Answers1

1

If you're looking for a tool to do this, you could use NDepend which can do what you're describing and also a lot more.

Or, if you're using ReSharper: Right-Click, "find usages"... (not useful for finding unused code indeed)

Stefan Paul Noack
  • 3,654
  • 1
  • 27
  • 38