3

Here's the class, which I want to get a list of all types used in it:

public class TestClass : MonoBehaviour{
    private UIManager _manager;

    private void Initialize(UIManager manager){
        _manager = manager;
    }
}

Then I thought that by running something like this:

    Assembly assembly = typeof (TestClass).Assembly;
    foreach (Type type in assembly.GetTypes()){
        Debug.Log(type.FullName);
    }

Would give me just the UIManager type. But instead it seems like it's giving me the list of all types used on my project.

How can I get just the types used on this class?

(as you can see TestClass inherits from MonoBehaviour, but I don't want the types used there, I want the types used specifically in TestClass).

Vasco
  • 83
  • 7
  • 1
    Why would you want to do this? What is your definition of "used"? – Dark Falcon Apr 08 '15 at 16:57
  • 1
    By "types", do you mean the Members and Methods that the class exposes? If so, you could probably use reflection for this. – Roberto Hernandez Apr 08 '15 at 16:58
  • By "used" I mean referenced somewhere in that class. And I want this to create a tool which lets me see if a certain class is used or not in my project. – Vasco Apr 08 '15 at 17:01

3 Answers3

1

If you want to know what types used for methods/fileds/properties - use basic reflection to enumerate each of the methods/fields and grab types used in declaration.

I.e. for method you'd call Type.GetMethods and than dig through MethodInfo properties like MethodInfo.ReturnType to get used types. Potentially go down recursively through each base class/interface to find all dependencies.

If you want to know what types used inside methods you'll need to use some sort of IL parser as reflection does not provide way to peek into method's body. Sample can be found in Get types used inside a C# method body.

Note that there are existing tools that provide similar functionality already - i.e. ReSahrper has "find code depending on module" and "find usages for types/methods/...".

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I really thought that the GetTypes() method did that for me...Oh well, thank you for the clarification, now it makes sense why I couldn't find any example of this. – Vasco Apr 08 '15 at 17:16
1

It's returning what is expected. Type.Assembly returns the assembly (.dll, .exe) in which the type is declared. In your case the assembly is your project's output. Therefore GetTypes will return all the types contained in that assembly.

What you want to do can be achieved by enumerating the methods and properties declared in your Type. This is done by calling the Type.GetMethods and Type.GetProperties method.

Something like this:

foreach(PropertyInfo prop in typeof(TestClass).GetProperties)
{
    //Access types.
}

foreach(MethodInfo method in typeof(TestClass).GetProperties)
{
    //Access types.
}
Ninglin
  • 146
  • 1
  • 9
1

You need System.Assembly.GetReferencedAssemblies(), thus:

using System.Reflection;
...
Assembly       assembly     = Assembly.LoadFile(@"c:\path\to\some\arbitrary-assembly.dll");
AssemblyName[] dependencies = assembly.GetReferencedAssemblies();

It is left as an exercise for the reader to construct the recursive tree walk to enumerate the full set of direct and indirect assemblies. One might note that this will require loading every referenced assembly. Consider doing this sort of examinination in a standalone process or loading the assemblies into a reflection-only context.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135