You need to enable JavaScript to run this app. C#反射为静态类无参静态Action事件订阅无参void方法的问题
优惠活动
大模型
产品
解决方案
定价
更多

C#反射为静态类无参静态Action事件订阅无参void方法的问题

问题根因与实现方案

现有代码的核心问题

  • 委托创建顺序错误:当前IsLoginCallback方法第一行就强制将所有方法转换为Action<ILoginSession>委托,遇到无参void方法时这一步就会直接抛出异常,根本无法执行到你编写的无参委托创建逻辑。
  • 未区分方法类型就传入null绑定目标:CreateDelegate方法的规则为:绑定静态方法时才可传入null作为target;绑定实例方法时必须传入方法所属类的实例,传null会直接抛出ArgumentException。你遍历方法时包含了实例方法,未做判断就传null,是报错的另一核心原因。
  • 签名未校验:创建委托前未校验方法返回值、参数列表是否匹配目标委托,会导致签名不匹配的运行时异常。

静态类静态事件的订阅逻辑

静态类定义的静态事件,本身就可直接通过类名访问,完全不需要类实例。你之前绑定带ILoginSession参数的回调时,使用VivoxLogin.xxxEvent += runtimeDelegate的写法是完全正确的,不存在“静态类无实例就无法订阅事件”的问题。

改造后的实现代码

调整IsLoginCallback的逻辑,先读取回调特性、再校验方法签名,最后按签名匹配对应委托与事件:

public static bool IsLoginCallback(MethodInfo method)
{
    // 先读取回调特性,无特性直接返回,避免无意义的委托创建
    var callbackAttr = method.GetCustomAttribute<LoginCallbackAttribute>(false);
    if (callbackAttr == null) return false;

    Delegate runtimeDelegate = null;
    // 仅处理返回值为void的方法
    if (method.ReturnType != typeof(void)) return false;

    var parameters = method.GetParameters();
    // 匹配带ILoginSession参数的回调签名
    if (parameters.Length == 1 && parameters[0].ParameterType == typeof(ILoginSession))
    {
        // 仅静态方法可传null绑定,不支持实例方法的话直接跳过
        if (!method.IsStatic) return false;
        runtimeDelegate = method.CreateDelegate(typeof(Action<ILoginSession>), null);
    }
    // 匹配无参回调签名
    else if (parameters.Length == 0)
    {
        if (!method.IsStatic) return false;
        runtimeDelegate = method.CreateDelegate(typeof(Action), null);
    }
    // 签名不匹配直接返回
    if (runtimeDelegate == null) return false;

    // 按特性配置绑定对应事件,注意委托类型匹配
    switch (callbackAttr.action)
    {
        case LoginAction.LogginIn:
            // 若为带参数委托,绑定到对应参数事件;无参委托绑定到对应无参事件
            if (runtimeDelegate is Action<ILoginSession> paramDelegate)
                VivoxLogin.VivoxLoggingIn += paramDelegate;
            // 取消下面的注释,若VivoxLoggingIn存在无参重载事件
            // else if (runtimeDelegate is Action noParamDelegate)
            //     VivoxLogin.VivoxLoggingInNoParam += noParamDelegate;
            return true;
        case LoginAction.LoggedIn:
            if (runtimeDelegate is Action<ILoginSession> paramDelegate)
                VivoxLogin.VivoxLoggedIn += paramDelegate;
            // else if (runtimeDelegate is Action noParamDelegate)
            //     VivoxLogin.VivoxLoggedInNoParam += noParamDelegate;
            return true;
        case LoginAction.LoggingOut:
            if (runtimeDelegate is Action<ILoginSession> paramDelegate)
                VivoxLogin.VivoxLoggingOut += paramDelegate;
            // else if (runtimeDelegate is Action noParamDelegate)
            //     VivoxLogin.VivoxLoggingOutNoParam += noParamDelegate;
            return true;
        case LoginAction.LoggedOut:
            if (runtimeDelegate is Action<ILoginSession> paramDelegate)
                VivoxLogin.VivoxLoggedOut += paramDelegate;
            // else if (runtimeDelegate is Action noParamDelegate)
            //     VivoxLogin.VivoxLoggedOutNoParam += noParamDelegate;
            return true;
    }
    return false;
}

可选优化项

  • 若完全不需要支持实例方法的回调,可在AddLoginRuntimeCallbackMethods的循环中直接添加过滤条件:if (!method.IsStatic) continue;,从根源避免无效的委托创建尝试。
  • 若需要支持实例方法回调,需额外实现实例的获取逻辑(比如依赖注入容器、单例类实例获取等),再将获取到的实例作为CreateDelegate的target参数传入。

内容的提问来源于stack exchange,提问作者John Murphy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.03 03:27:06