- Home /
C# Using Reflection to Automate finding classes
This may be multiple things I am asking for, but I think they all relate to reflection in some way.
I am making a visual scripting tool/node editor. I have a system setup already in code that upon opening an editor window saves a node script, then it can be loaded by another script on a gameobject and used. What I want to do is to automate future use when I start doing the GUI; as in, I don't want to have to have a huge switch case or list that I have to add to every time I add a new node type.
I want to use reflection, or something, that will look for every class I make that extends from the abstract class 'Node' and can tell all of their properties and give me some way to make an instance of it on request. This way creating new node types is as simple as duplicating a basic existing one, changing the name and data, and the editor I coded does it for me.
How should I go about doing this? Google has not seemed to help me much XD
$$anonymous$$ainly I want to automatically find classes extended from an abstract Node that extends from a ScriptableObject
Answer by Bunny83 · Jun 09, 2015 at 04:59 AM
Just use this extension method:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class ReflectionHelpers
{
public static System.Type[] GetAllDerivedTypes(this System.AppDomain aAppDomain, System.Type aType)
{
var result = new List<System.Type>();
var assemblies = aAppDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var types = assembly.GetTypes();
foreach (var type in types)
{
if (type.IsSubclassOf(aType))
result.Add(type);
}
}
return result.ToArray();
}
public static System.Type[] GetAllDerivedTypes<T>(this System.AppDomain aAppDomain)
{
return GerAllDerivedTypes(aAppDomain, typeof(T));
}
public static System.Type[] GetTypesWithInterface(this System.AppDomain aAppDomain, System.Type aInterfaceType)
{
var result = new List<System.Type>();
var assemblies = aAppDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var types = assembly.GetTypes();
foreach (var type in types)
{
if (aInterfaceType.IsAssignableFrom(type))
result.Add(type);
}
}
return result.ToArray();
}
public static System.Type[] GetTypesWithInterface<T>(this System.AppDomain aAppDomain)
{
return GetTypesWithInterface(aAppDomain, typeof(T));
}
}
Just place this class somewhere in your project. Then just use:
var types = System.AppDomain.CurrentDomain.GetAllDerivedTypes(typeof(YourBaseClass));
edit
I've just added the method "GetTypesWithInterface" which also works with interface types. So you can find all classes which implement a given interface.
var types = System.AppDomain.CurrentDomain.GetTypesWithInterface(typeof(IMyInterface));
Thanks! Now I need to post one more question for another roadblock lol
What can an array of types be used for? I can't figure out how to actually do anything with this array.
Well, depending on the nature of your class you can either attach them as components to a gameobject (in case the types are actually $$anonymous$$onoBehaviours) or if they are ordinary classes you can create an instance of those classes by using the Activator class.
Since we're only interested in classes derived from our base class we can safely cast the created objects to our base class. So we can work normally with polymorphism.
I just realised that the "GetAllDerivedTypes" method only checks for classes that are derived from a base class. I'll add another method that works with interfaces as well which is probably more useful.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
invoke method with reflection C# 3 Answers
Editor script private fields and method best practices 0 Answers
Custom Editor Properties Revert to Defaults on Play 4 Answers