10

Using System.Reflection, I can get all methods from a specific class

I need know what are the references to these methods. For example: in Visual Studio, if you want the references of specific object

  • right click on the object and select "Find All References"
  • Visual Studio show the references of this selected object

I want make the same, but from code with reflection or another way.

Can I do this?

Nathan Koop
  • 24,803
  • 25
  • 90
  • 125
Cristhiam Teran
  • 117
  • 1
  • 3
  • 4
    **Why** are people upvoting this question? It's full of typos and grammatical errors, and it's asking to do something that at best has almost no practical purpose (unless you're trying to re-invent Reflector) and most likely is leading to its own DailyWTF article in the distant future. – Aaronaught Jan 31 '10 at 17:41
  • @Aaronaught Up-VotdEd just fo ryou only :) – Joseph Gordon Feb 01 '10 at 00:53
  • 1
    @Aaronaught (and those who up-voted his comment) bad "engrish" annoys me too, but through my years of hanging out at stack exchange I have found out that the appropriate thing to do in these cases is considered to be to edit the question to fix the "engrish" rather than to complain. – Mike Nakis Mar 23 '13 at 16:20
  • @Aaronaught as for the usefulness of the question, or the motives of the person asking it, nobody can judge that. The only thing of concern is whether this is a valid question, and it certainly is. Besides, regardless of what Christiam Teran is going to do with the answers, the pursuit of knowledge for the sake of knowledge is a noble pursuit indeed. – Mike Nakis Mar 23 '13 at 16:24
  • That question: c# reflection and find all references http://stackoverflow.com/questions/5490025/c-sharp-reflection-and-find-all-references is a duplicate of this one, because that one has a later date than this one. Nonetheless, that one has received a very good answer! – Mike Nakis Mar 23 '13 at 16:27

4 Answers4

5

This cannot be done with reflection. Reflection is a tool for inspecting metadata and assemblies. In order to find all references to a given method / type, you'd need to inspect the underlying IL of an assembly. Reflection only has very limited IL capabilities (simply returns it as a byte array). You'll need to custom inspect that byte stream in order to gather any context about what it's referencing.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

That's not something that's directly accessible via runtime reflection on a specific class. You will have to introspect the entire source code tree or resulting IL to determine if any references to a particular method with the same name are the right overload and signature for the method you're trying to find references to.

Furthermore, without additional work, you're never going to find references to a specific method that are themselves invoked via reflection. (This is one reason why obfuscating that kind of code is challenging and error-prone.)

John Feminella
  • 303,634
  • 46
  • 339
  • 357
2

If you're just looking to find the references for informational purposes, Reflector has that feature.

http://www.red-gate.com/products/reflector/

Chris Stavropoulos
  • 1,766
  • 13
  • 27
0

Microsoft released the Common Compiler Infrastructure projects under an open source license. These projects aim to support many compiler-related features, including assembly analysis like you're referring to. The documentation is limited, so you'll need to have a thorough understanding of ECMA-335 (Common Language Infrastructure) to effectively use it for your purposes.

There are no magic code samples here. This is a large and quite complicated task where you'll be on your own most of the way.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280