在这个生成的路由器接收 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;
}
}
}
生成代码仅作示例,不代表任何特定模型