审查生成的反射命令路由器

来自 反射
C# 14 / .NET 10 高级 6分钟 找出 5处问题

在这个生成的路由器接收 HTTP 端点命令前,对它进行审查。

分派白名单内的单参数命令,只解析一次精确签名,按明确契约解析值,保留目标异常,并限制注册表的所有权与大小。

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;
        }
    }
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误