Vasco
Vasco

Reputation: 83

Get types used inside a specific class

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

Upvotes: 3

Views: 2196

Answers (3)

Nicholas Carey
Nicholas Carey

Reputation: 74267

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.

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

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/...".

Upvotes: 1

Ninglin
Ninglin

Reputation: 146

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

Upvotes: 1

Related Questions