コード例 #1
0
    private void TestGeneric()
    {
        const string CLASS_NAME = "HotUpdate.TestGeneric";

        object obj = appdomain.Instantiate(CLASS_NAME, null);

        // 方式1
        IType stringType = appdomain.GetType(typeof(string));

        IType[] genericArguments = new IType[] { stringType };
        appdomain.InvokeGenericMethod(CLASS_NAME, "Print", genericArguments, obj, "hello world");

        // 方式2
        IType type = appdomain.LoadedTypes[CLASS_NAME];

        List <IType> listParam   = new List <IType>();
        IType        stringType2 = appdomain.GetType(typeof(string));
        IType        intType2    = appdomain.GetType(typeof(int));

        listParam.Add(stringType2);
        listParam.Add(intType2);

        IType[] genericArguments2 = new IType[] { stringType2, intType2 };
        IMethod method            = type.GetMethod("Format", listParam, genericArguments2);

        string format = (string)appdomain.Invoke(method, obj, new object[] { "hello world", 1024 });

        Debug.LogFormat("ILManager.TestGeneric().format = {0}", format);
    }
コード例 #2
0
    private void OnHotFixLoaded()
    {
        appdomain.Invoke("HotFix_Project.InstanceClass", "StaticFunTest", null, null);


        //IType type = appdomain.LoadedTypes ("HotFix_Project.InstanceClass");
        IType   type   = appdomain.LoadedTypes["HotFix_Project.InstanceClass"];
        IMethod method = type.GetMethod("StaticFunTest", 0);

        appdomain.Invoke(method, null, null);

        Debug.Log("指定参数类型来获得IMethod");
        IType intType = appdomain.GetType(typeof(int));


        //参数类型列表
        List <IType> paramList = new List <IType>();

        paramList.Add(intType);
        IMethod method2 = type.GetMethod("StaticFunTest2", paramList, null);

        appdomain.Invoke(method2, null, 911);

        Debug.Log("实例化热更里的类");
        //第一种方式

        ILTypeInstance obj = appdomain.Instantiate("HotFix_Project.InstanceClass", new object[] { 111 });
        //第二种方式
        object obj2 = ((ILType)type).Instantiate();

        Debug.Log("调用成员方法");
        int id = (int)appdomain.Invoke("HotFix_Project.InstanceClass", "get_ID", obj, null);

        Debug.Log("!! HotFix_Project.InstanceClass.ID = " + id);
        id = (int)appdomain.Invoke("HotFix_Project.InstanceClass", "get_ID", obj2, null);
        Debug.Log("!! HotFix_Project.InstanceClass.ID = " + id);
        Debug.Log("!! cache method  StaticFunTest :");
        appdomain.Invoke(method, obj, null);

        Debug.Log("调用泛型方法");

        IType stringType = appdomain.GetType(typeof(string));

        IType[] genericArguments = { stringType };
        appdomain.InvokeGenericMethod("HotFix_Project.InstanceClass", "GenericMethod", genericArguments, null, "string generic method");

        Debug.Log("获取泛型方法的IMethod");
        paramList.Clear();
        paramList.Add(intType);
        genericArguments = new IType [] { intType };
        method           = type.GetMethod("GenericMethod", paramList, genericArguments);
        appdomain.Invoke(method, obj, 300);

        Debug.Log("获取泛型方法的IMethod 2");
        genericArguments = new IType[] { intType };
        method           = type.GetMethod("GenericMethod2", null, genericArguments);
        appdomain.Invoke(method, obj, null);

        Debug.Log("获取泛型方法的IMethod 3");
        genericArguments = new IType[] { intType };
        method           = obj.Type.GetMethod("GenericMethod3", null, genericArguments);
        appdomain.Invoke(method, obj, null);
    }