Review a generated reflection command router

from Reflection
C# 14 / .NET 10 advanced 6 min 5 issues to find

Review this generated router before it accepts commands from an HTTP endpoint.

Dispatch allowlisted one-argument commands, resolve exact signatures once, parse values by an explicit contract, preserve target exceptions, and bound registry ownership.

csharp
using System;
using System.Reflection;

public static class CommandRouter
{
    public static object? Dispatch(object target, string methodName, string rawValue)
    {
        Type type = target.GetType();
        MethodInfo method = type.GetMethod(methodName,
            BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)!;
        Type parameterType = method.GetParameters()[0].ParameterType;
        object? argument = Convert.ChangeType(rawValue, parameterType);

        try
        {
            return method.Invoke(target, [argument]);
        }
        catch (Exception error)
        {
            Console.WriteLine(error.Message);
            return null;
        }
    }
}

generated code is illustrative, not from any one model

Open in playground
Report an error