ILRuntime使用(注册跨域委托)

1.Action

//比如system.Action<int>
appdomain.DelegateManager.RegisterMethodDelegate<int>();

2.Func

//比如system.Func<int,bool>
appdomain.DelegateManager.RegisterFunctionDelegate<int,bool>();

3.自定义委托

/*比如
public delegate void TestDelegateMethod(int a);
public static TestDelegateMethod TestMethodDelegate;
*/
//先适配action
appdomain.DelegateManager.RegisterMethodDelegate<int>();
//再转换适配
appdomain.DelegateManager.RegisterDelegateConvertor<TestDelegateMethod>((action) =>
{
    return new TestDelegateMethod((a) =>
    {         
        ((System.Action<int>)action)(a);
    });
});

4.常用的unity内置委托

//UGUI按钮事件        appdomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction>((action) =>
        {
            return new UnityEngine.Events.UnityAction(() =>
            {
                ((System.Action)action)();
            });
        });
 
 
        //sort委托适配
        //类比较
        appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, ILTypeInstance,int>();
        appdomain.DelegateManager.RegisterDelegateConvertor<System.Comparison<ILTypeInstance>>((action) =>
        {
            return new System.Comparison<ILTypeInstance>((a,b) =>
            {
                return ((System.Func<ILTypeInstance, ILTypeInstance,int>)action)(a,b);
            });
        });
        //int比较
        appdomain.DelegateManager.RegisterFunctionDelegate<int, int, int>();
        appdomain.DelegateManager.RegisterDelegateConvertor<System.Comparison<int>>((action) =>
        {
            return new System.Comparison<int>((a, b) =>
            {
                return ((System.Func<int, int, int>)action)(a, b);
            });
        });
 
 
        //linq(以orderBy为例)
        appdomain.DelegateManager.RegisterFunctionDelegate<int, int>();//int排序
        appdomain.DelegateManager.RegisterFunctionDelegate<ILTypeInstance, int>();//类中的int排序

 

评论

 

关注公众号

可用手机学习

获取最新课程