Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can get all reference with Reflection + C#

Tags:

c#

reflection

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?

like image 419
Cristhiam Teran Avatar asked Jan 31 '10 16:01

Cristhiam Teran


3 Answers

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.

like image 128
JaredPar Avatar answered Oct 18 '22 23:10

JaredPar


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.)

like image 2
John Feminella Avatar answered Oct 18 '22 21:10

John Feminella


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

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

like image 2
Chris Stavropoulos Avatar answered Oct 18 '22 22:10

Chris Stavropoulos