egametang/ET · critical · Exception

ConsoleHandler handler not inherit IConsoleHandler class: {o

Error message

ConsoleHandler handler not inherit IConsoleHandler class: {obj.GetType().FullName}

What it means

Thrown during ConsoleDispatcher initialization when a type decorated with [ConsoleHandler] is instantiated but does not implement the IConsoleHandler interface. The dispatcher casts the created instance via `as IConsoleHandler`; a null result means the contract is not satisfied and the handler cannot be registered.

Source

Thrown at Packages/cn.etetet.console/Scripts/Model/Server/ConsoleDispatcher.cs:31

        {
            HashSet<Type> types = CodeTypes.Instance.GetTypes(typeof (ConsoleHandlerAttribute));

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(ConsoleHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }

                ConsoleHandlerAttribute consoleHandlerAttribute = (ConsoleHandlerAttribute)attrs[0];

                object obj = Activator.CreateInstance(type);

                IConsoleHandler iConsoleHandler = obj as IConsoleHandler;
                if (iConsoleHandler == null)
                {
                    throw new Exception($"ConsoleHandler handler not inherit IConsoleHandler class: {obj.GetType().FullName}");
                }
                this.handlers.Add(consoleHandlerAttribute.Mode, iConsoleHandler);
            }
        }

        public IConsoleHandler Get(string key)
        {
            return this.handlers[key];
        }
    }
}

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Make the class implement IConsoleHandler (declare the interface and all its members).
  2. Remove [ConsoleHandler] from the type if it is not actually a handler.
  3. Re-scan so the dispatcher picks up the corrected type.

Example fix

// before
[ConsoleHandler("kick")]
public class KickCommand { /* no interface */ }
// after
[ConsoleHandler("kick")]
public class KickCommand : IConsoleHandler
{
    public void Run(string[] args) { /* ... */ }
}
Defensive patterns

Strategy: type-guard

Validate before calling

// in a bootstrap test
foreach (Type t in CodeTypes.Instance.GetTypes(typeof(ConsoleHandlerAttribute)))
    Assert.IsTrue(typeof(IConsoleHandler).IsAssignableFrom(t), $"{t.FullName} must implement IConsoleHandler");

Type guard

static bool IsConsoleHandler(Type t) => typeof(IConsoleHandler).IsAssignableFrom(t);

Prevention

When it happens

Trigger: Applying [ConsoleHandler(...)] to a class that lacks `: IConsoleHandler`; implementing the interface's members without declaring the interface; the interface was renamed/removed during a refactor.

Common situations: A developer adds a console command handler class and forgets the interface; refactor splits IConsoleHandler and the type only implements part of it.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/532cd226712b5138. Report an issue: GitHub.