Skip to content

Instance Helper

shmellyorc edited this page Sep 12, 2026 · 3 revisions

Instance Helper

InstanceHelper creates compatible classes dynamically by type name, Type, or an existing object's type.

It works well with Discoverable System, but it does not require [Discoverable].

Why Use It?

Suppose your game defines:

public interface IEnemyFactory
{
}

with several implementations.

If you only know the class name at runtime:

IEnemyFactory factory =
    InstanceHelper.CreateInstance<IEnemyFactory>(
        "GoblinFactory",
        ignoreCase: true,
        args: null);

The result is null if VOID cannot find or construct a compatible type.

Create by Name

var factory =
    InstanceHelper.CreateInstance<IEnemyFactory>(
        "GoblinFactory",
        true,
        null);

Constructor arguments:

var service =
    InstanceHelper.CreateInstance<IGameService>(
        "SaveService",
        true,
        new object[]
        {
            "profile-1",
            3
        });

Activator.CreateInstance is used internally, so the supplied argument list needs to match a constructor that can be invoked.

TryCreateInstance

if (InstanceHelper.TryCreateInstance<IEnemyFactory>(
        "GoblinFactory",
        true,
        null,
        out var factory))
{
    // Use factory.
}

Create From a Type

This is the common pairing with DiscoverableHelper:

Type type =
    DiscoverableHelper.FindSingleByName<IGameplayModule>(
        "Weather");

IGameplayModule module =
    InstanceHelper.CreateInstanceFromType<IGameplayModule>(
        type,
        null);

Try version:

if (InstanceHelper.TryCreateInstanceFromType<IGameplayModule>(
        type,
        null,
        out var module))
{
    module.Initialize();
}

Create the Same Runtime Type as an Object

IEnemyFactory copy =
    InstanceHelper.CreateInstanceFromObject<IEnemyFactory>(
        existingFactory,
        null);

Try version:

if (InstanceHelper.TryCreateInstanceFromObject<IEnemyFactory>(
        existingFactory,
        null,
        out var copy))
{
    // Use copy.
}

Type Lookup

CreateInstance<T>(name, ...) scans loaded non-framework assemblies for a class whose full name or simple name matches and that can be assigned to T.

Successful lookups are cached.

Failed lookups are also cached so an invalid name does not repeatedly scan every assembly.

Runtime Assembly Loading

If your own code loads another assembly at runtime, refresh the instance helper afterward:

Assembly.LoadFrom("Plugins/MyPlugin.dll");

InstanceHelper.RefreshAssemblies();
DiscoverableHelper.InvalidateCaches();

That only refreshes VOID's lookup/discovery caches. It does not make arbitrary DLL loading safe.

If you build a plugin/mod system, you are responsible for:

  • deciding where assemblies come from
  • validating/trusting them
  • loading them
  • defining their lifecycle
  • handling compatibility/versioning
  • deciding whether unrestricted code execution is acceptable

See Mod Support.

Complete Example

public interface IGameplayModule
{
    void Initialize();
}

[Discoverable(
    Name = "Weather",
    Category = "Gameplay")]
public sealed class WeatherModule : IGameplayModule
{
    public void Initialize()
    {
        Console.WriteLine("Weather ready");
    }
}

IReadOnlyList<Type> types =
    DiscoverableHelper.FindAll<IGameplayModule>();

foreach (Type type in types)
{
    if (InstanceHelper.TryCreateInstanceFromType<IGameplayModule>(
            type,
            null,
            out var module))
    {
        module.Initialize();
    }
}

Discoverable System · Back to Home

Clone this wiki locally