ILRuntime 問題總匯


1.在調用委托時報錯KeyNotFoundException: Cannot find Delegate Adapter for:HotFix_Project.ui_page_logingame.Go

代碼如下:在hotfix中

protected override void _PageOpenImpl()
        {
            UnityEngine.Debug.Log("11111111111111111111111111111111111111111111");
            ObserverManager.RegisterNotification(NotificationConstant.Login_Succ, Go);
        }
主工程中ObserverManager.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using CSharpEngine.Artifact;

public delegate void NotificationAction(EventParam param);
public class ObserverManager
{
    // 觀察者通知池
    private static Dictionary<string, List<NotificationAction>> _notifierDict = new Dictionary<string, List<NotificationAction>>();

    /// <summary> 
    /// 注冊觀察者通知
    /// </summary>
    public static void RegisterNotification(string _note,NotificationAction _obs)
    {
        if (!_notifierDict.ContainsKey(_note))
        {
            _notifierDict.Add(_note,new List<NotificationAction>());
        }
        _notifierDict[_note].Add(_obs);
    }

    /// <summary> 
    /// 撤銷觀察者通知
    /// </summary>
    public static void UnregisterNotification(string _note, NotificationAction _obs)
    {
        if (_notifierDict.ContainsKey(_note))
        {
            for (int i = 0; i < _notifierDict[_note].Count; i++)
            {
                if (_notifierDict[_note][i] == _obs)
                {
                    _notifierDict[_note].RemoveAt(i);
                    break;
                }
            }
        }
    }
    internal static void RegisterNotification(object setReady)
    {
        throw new NotImplementedException();
    }

    /// <summary> 
    /// 發送信息
    /// </summary>
    public static void SendNotification(string _note, EventParam _param)
    {
        if (_notifierDict.ContainsKey(_note))
        {
            for (int i = 0; i < _notifierDict[_note].Count; i++)
            {
                _notifierDict[_note][i]?.Invoke(_param);
            }
        }
    }
    /// <summary> 
    /// 發送信息
    /// </summary>
    public static void SendNotificationC(string note_, EventParam data_)
    {
        SendNotification(note_, data_);
    }
}

 

其中用到了
EventParam  和
NotificationAction必須在主工程注冊為委托才行。
注冊代碼如下:
        manager.RegisterMethodDelegate<CSharpEngine.Artifact.EventParam>();
        manager.RegisterMethodDelegate<NotificationAction>();

注冊后調用委托又報其它錯:

 

 這時要添加Notification的轉換器才行

在主工程代碼如下

appdomain.DelegateManager.RegisterDelegateConvertor<NotificationAction>((action) =>
        {
            return new NotificationAction((o) =>
            {
                ((System.Action)action)();
            });
        });

OK 跑過了!!!

參考文章:https://www.cnblogs.com/bbdr/articles/14303840.html

然后發送觀察者的時候又發現報錯:

 

錯誤代碼為:

 

 

將其修改為:

 

 

好了,這次真的可以了。



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM