- Home /
Is there a way to mimic the 'Find References' functionality of MonoDevelop?
We all know how useful MonoDevelop's 'Find References' feature is for certain scenarios where you'd want to quickly find all scripts and EXACT lines in the whole solution that reference or call a specific method/function.
I just want to get your ideas on how one could accomplish this in Unity. I need to create a tool that essentially provides the reverse of CollectDependencies (http://docs.unity3d.com/ScriptReference/EditorUtility.CollectDependencies.html), and yet, does so on a lower level (i.e. finding exact lines of code in the whole project that call the function).
By the way, the closest solution I could find is probably this: http://wiki.unity3d.com/index.php/UnityAssetXrefs. That utility, however, doesn't allow me to specify which function I wanted to find references of. Only the class/script. It's close though...
Answer by Bunny83 · Sep 26, 2014 at 09:59 AM
Yes, it is possible, but you have to dive a bit deeper into reflection. I don't post a full working example here, just the basic steps you need:
AppDomain.CurrentDomain to get the current app domain
Then use GetAssemblies to get a list of all loaded assemblies. You might want to filter that list as you won't find references to your own classes / methods / fields in mscorlib.dll, UnityEngine.dll, ... and searching the whole appdomain would probably take hours :)
On the remaining assemblies you would call GetTypes to get a list of all classes defines in that assembly. So you end up with a huge list of System.Type references.
On each type you would call GetMethods to get all MethodInfos of all methods in those classes. Keep in mind to use the BindingFlags version if you also want to search private / static methods
The MethodInfo class (or to be more precise it's base class MethodBase) has a method called GetMethodBody which returns a MethodBody object
There are many "frameworks" to parse the IL code into OpCodes. This might also help
Once you have the OpCodes you can search the OpCode parameters / immediate values for a certain MethodInfo / FieldInfo.
That's all :D
that is a pretty extensive answer Bunny83. Thanks! I will try this out asap and most probably will come back to this question to check your answer as the accepted one. thumbsup
after reading stuff on .NET, IL, and Reflection, I cannot believe I made it all the way up to this point in my coding life where I have not yet (not even once) dived into all this which seems pretty straightforward and easy as vexe points out... I feel like a jackass, lol... well, something new to learn everyday!
Wow. Impressive. It will be a long time before I call any code using reflection straightforward and easy. :)
Your answer
Follow this Question
Related Questions
using System.Security.Cryptography.Xml in Unity 0 Answers
A node in a childnode? 1 Answer
MonoDevelop not Interfacing with Unity 2 Answers
how to debug in monodevelop 1 Answer
There is no preferences option in MonoDevelop-Unity 1 Answer