How to convert a string to Type?

I am writing a dynamic game console. You can add commands to it using ScriptableObjects. In that ScriptsableObjects you can assign a command (/command) and what it should do. Because I just started working on this thing you can only do 3 things as if now, "destroy "(destroys an objects by name), "create’ (creates an object by name), and “destroyall”. Destroy and create are working, but destroyall doesn’t. Because I want this console to be dynamic, I need to come up with a way to convert a string into a type.

Here is how it works:

When you enter a command in the console and press enter, it will split that command to an array. When you do /yourcommand enemymovement, enemy movement is the second element in that array. From the second element in the array I want it to look for objects of that type. But since the second element is a String and not a Type, I can’t do "GameObjects.FindObjectsOfType (command is the name of the array).

How can I make this work without using a switch or if like :
If (command[1] == “enemymovement”)
{
destroy …
}
else if (…) etc.

@levigeers

Try something like this:

private Type StringToType(string typeAsString)
{
     Type typeAsType = Type.GetType(typeAsString);
     return typeAsType;
}

You would use [System.Type.GetType()][1]. However be warned that it can only return types that are defined either in the mscorlib or the current executing assembly. If your classes might be located in multiple assemblies you have to iterate all loaded assemblies. This can be done with the AppDomain class. The static property System.AppDomain.CurrentDomain gives you access to the current AppDomain. System.AppDomain.CurrentDomain.GetAssemblies() gives you a list of all currently “Assemblies” in your app domain. Now you can use “GetType” of the Assembly class to get a certain type by name.

However GetType can only return public classes. If the class you search is private or internal your only option is to use GetTypes() of each Assembly to get a list of all types defined inside the assembly. So ou have to manually iterate through the array to find your class. Keep in mind that there could be several classes with the same name but located in different namespaces. If you only want to find your own classes that’s usually not a problem as normal scripts are in the global namespace.

edit

// this would be the simple version
public static UnityEngine.Object[] FindObjectsOfTypeByName(string aClassName)
{
    var type = System.Type.GetType(aClassName);
    return UnityEngine.Object.FindObjectOfType(type);
}

// heres the more advanced version
public static UnityEngine.Object[] FindObjectsOfTypeByName(string aClassName)
{
    var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
    for(int i = 0; i < assemblies.Length; i++)
    {
        var types = assemblies*.GetTypes();*

for(int n = 0; n < types.Length; n++)
{
if (typeof(UnityEngine.Object).IsAssignableFrom(types[n]) && aClassName == types[n].Name)
return UnityEngine.Object.FindObjectsOfType(types[n]);
}
}
return new UnityEngine.Object[0];
}
[1]: Type.GetType Method (System) | Microsoft Learn