ILRuntime学习记录 -加载以及函数调用

官方文档: https://ourpalm.github.io/ILRuntime/public/v1/guide/tutorial.html

域:游戏运行、存在2个域,运行域和热更域
相关问题:跨域委托,跨域继承(需要编写基类适配器),IL2CPP裁剪问题
热更DLL调用主域方法时,CLR绑定可以提高效率


加载

  • 加载 this.appDomain.LoadAssembly(dllStream, pdbStream, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());

方法调用

  • 调用静态方法 appdomain.Invoke("类名", "方法名", 对象引用, 参数列表)
  • 通过IMethod调用方法
		IType type = appdomain.LoadedTypes["HotFix_Project.InstanceClass"];
		IMethod method = type.GetMethod("StaticFunTest2", 参数数量);
		appdomain.Invoke(method, null, 123);
  • 通过无GC Alloc方式调用方法
        using (var ctx = appdomain.BeginInvoke(method))
        {
            ctx.PushInteger(123);
            ctx.Invoke();
        }
  • 指定参数类型来获得IMethod
	IType intType = appdomain.GetType(typeof(int));
	List<IType> paramList = new List<ILRuntime.CLR.TypeSystem.IType>();
	paramList.Add(intType);
	method = type.GetMethod("StaticFunTest2", paramList, null);
	appdomain.Invoke(method, null, 456);

实例化

  • 实例化热更类
	//方法一
	Object obj1 = appdomain.Instantiate("HotFix_Project.InstanceClass", new object[] { 233 });
	//方法二
	Object obj2 = ((ILType)type).Instantiate();
  • 调用成员方法
	method = type.GetMethod("get_ID", 0);
    using (var ctx = appdomain.BeginInvoke(method))
    {
        ctx.PushObject(obj); // 传入实例
        ctx.Invoke(); // 执行
        int id = ctx.ReadInteger(); //获得结果
        Debug.Log("!! HotFix_Project.InstanceClass.ID = " + id);
    }
  • 调用泛型方法
	IType stringType = appdomain.GetType(typeof(string));
	IType[] genericArguments = new IType[] { stringType };
	appdomain.InvokeGenericMethod("HotFix_Project.InstanceClass", "GenericMethod", genericArguments, null, "TestString");
  • 获取泛型方法的IMethod
	List<IType> paramList = new List<ILRuntime.CLR.TypeSystem.IType>();
	IType intType = appdomain.GetType(typeof(int));
	paramList.Add(intType);
	genericArguments = new IType[] { intType };
	method = type.GetMethod("GenericMethod", paramList, genericArguments);
	appdomain.Invoke(method, null, 33333);
  • 通过反射创建实例
	var it = appdomain.LoadedTypes["HotFix_Project.InstanceClass"];
	var type = it.ReflectionType;
	var ctor = type.GetConstructor(new System.Type[0]);
	var obj = ctor.Invoke(null);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值