public static BaseReflectedMethodNode GetMethodNode(MethodInfo targetMethod, ReflectedMethodRegistrationOptions options)
        {
            ParametresDef paramDef;

            if (!ReflectedNodesHelper.InitParams(targetMethod, out paramDef))
            {
                return(null);
            }
#if !NET_STANDARD_2_0 && (UNITY_EDITOR || (!ENABLE_IL2CPP && (UNITY_STANDALONE || UNITY_ANDROID || UNITY_WSA)))
            var jit = new JitMethodNode();
            jit.options = options;
            if (jit.Init(targetMethod, paramDef))
            {
                return(jit);
            }
#endif
            if (OnGetAotReflectedMethodNode != null)
            {
                var eventAot = OnGetAotReflectedMethodNode(targetMethod);
                if (eventAot != null)
                {
                    eventAot.options = options;
                    if (eventAot.Init(targetMethod, paramDef))
                    {
                        return(eventAot);
                    }
                }
            }
            var aot = new PureReflectedMethodNode();
            aot.options = options;
            return(aot.Init(targetMethod, paramDef) ? aot : null);
        }
        public static BaseReflectedFieldNode GetFieldNode(FieldInfo targetField)
        {
            ParametresDef paramDef;

            if (!ReflectedNodesHelper.InitParams(targetField, out paramDef))
            {
                return(null);
            }
#if !NET_STANDARD_2_0 && (UNITY_EDITOR || (!ENABLE_IL2CPP && (UNITY_STANDALONE || UNITY_ANDROID || UNITY_WSA)))
            var jit = new JitFieldNode();
            if (jit.Init(targetField, paramDef))
            {
                return(jit);
            }
#endif
            if (OnGetAotReflectedFieldNode != null)
            {
                var eventAot = OnGetAotReflectedFieldNode(targetField);
                if (eventAot != null && eventAot.Init(targetField, paramDef))
                {
                    return(eventAot);
                }
            }
            var aot = new PureReflectedFieldNode();
            return(aot.Init(targetField, paramDef) ? aot : null);
        }
        public static BaseReflectedExtractorNode GetExtractorNode(Type targetType, bool isStatic, MemberInfo[] infos)
        {
            ParametresDef paramsDef;

            if (!ReflectedNodesHelper.InitParams(targetType, isStatic, infos, out paramsDef))
            {
                return(null);
            }
#if UNITY_EDITOR || (!ENABLE_IL2CPP && (UNITY_STANDALONE || UNITY_ANDROID || UNITY_WSA))
            var jit = new JitExtractorNode();
            if (jit.Init(paramsDef, targetType))
            {
                return(jit);
            }
#endif
            if (OnGetAotExtractorNode != null)
            {
                var eventAot = OnGetAotExtractorNode(targetType, isStatic, infos);
                if (eventAot != null && eventAot.Init(paramsDef, targetType))
                {
                    return(eventAot);
                }
            }
            var aot = new PureReflectedExtractorNode();
            return(aot.Init(paramsDef, targetType) ? aot : null);
        }
Ejemplo n.º 4
0
        private void CreateDelegat()
        {
            var key = ReflectedNodesHelper.GetGeneratedKey(methodInfo);

            if (Delegates.TryGetValue(key, out delegat) && delegat != null)
            {
                return;
            }

            DynamicMethod dynamicMethod = new DynamicMethod(methodInfo.Name + "_Dynamic", null, DynParamTypes, typeof(JitMethodNode));
            ILGenerator   ilGen         = dynamicMethod.GetILGenerator();
            int           instanceId    = -1;
            int           returnId      = -1;

            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                var def   = param.paramDef;
                //declare local variables in method
                ilGen.DeclareLocal(param.GetCurrentType());
                if (def.paramMode == ParamMode.Instance)
                {
                    instanceId = i;
                }
                if (def.paramMode == ParamMode.Result)
                {
                    returnId = i;
                }
            }
            //store values to local variables
            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                //load first argument of method to stack, in our situation it "delegateParams" array
                ilGen.Emit(OpCodes.Ldarg, 0);
                //load current array index to stack
                ilGen.Emit(OpCodes.Ldc_I4, i);
                //load reference for delegateParams[i] to stack
                ilGen.Emit(OpCodes.Ldelem_Ref);
                //load value for delegateParams[i].value to stack
                ilGen.Emit(OpCodes.Ldfld, param.ValueField);
                //set local variable to loaded value
                ilGen.Emit(OpCodes.Stloc, i);
            }
            if (instanceId >= 0)
            {
                //load instance local to stack for use call on it
                ilGen.Emit(delegateParams[instanceId].GetCurrentType().RTIsValueType() ? OpCodes.Ldloca : OpCodes.Ldloc, instanceId);
            }
            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                var def   = param.paramDef;
                if (def.paramMode != ParamMode.Instance && def.paramMode != ParamMode.Result)
                {
                    ilGen.Emit(def.paramMode == ParamMode.In ? OpCodes.Ldloc : OpCodes.Ldloca, i);
                }
            }
            if (instanceId < 0 || delegateParams[instanceId].GetCurrentType().RTIsValueType())
            {
                //use Call opcode, because value types and statics methods cannot be virtual or overrided, result (if exist) will stored into stack
                ilGen.Emit(OpCodes.Call, methodInfo);
            }
            else
            {
                //use Callvirt opcode, because non value types and non statics methods can be virtual or overrided, result (if exist) will stored into stack
                ilGen.Emit(OpCodes.Callvirt, methodInfo);
            }
            if (returnId >= 0)
            {
                //set result of code to loacal var
                ilGen.Emit(OpCodes.Stloc, returnId);
            }
            //return local vals to our array
            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                //load first argument of method to stack, in our sytuation it "delegateParams" array
                ilGen.Emit(OpCodes.Ldarg, 0);
                //load current array index to stack
                ilGen.Emit(OpCodes.Ldc_I4, i);
                //load reference for delegateParams[i] to stack
                ilGen.Emit(OpCodes.Ldelem_Ref);
                //load value for local val
                ilGen.Emit(OpCodes.Ldloc, i);
                //set local variable to loaded value
                ilGen.Emit(OpCodes.Stfld, param.ValueField);
            }
            ilGen.Emit(OpCodes.Ret);
            delegat        = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate));
            Delegates[key] = delegat;
        }
Ejemplo n.º 5
0
        private void CreateDelegat()
        {
            var key = ReflectedNodesHelper.GetGeneratedKey(constructorInfo);

            if (Delegates.TryGetValue(key, out delegat) && delegat != null)
            {
                return;
            }

            DynamicMethod dynamicMethod = new DynamicMethod("Constructor_Dynamic", null, DynParamTypes, typeof(JitMethodNode));
            ILGenerator   ilGen         = dynamicMethod.GetILGenerator();
            int           returnId      = -1;

            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                var def   = param.paramDef;
                //declare local variables in method
                ilGen.DeclareLocal(param.GetCurrentType());
                if (def.paramMode == ParamMode.Result)
                {
                    returnId = i;
                }
            }
            //store values to local variables
            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                //load first argument of method to stack, in our situation it "delegateParams" array
                ilGen.Emit(OpCodes.Ldarg, 0);
                //load current array index to stack
                ilGen.Emit(OpCodes.Ldc_I4, i);
                //load reference for delegateParams[i] to stack
                ilGen.Emit(OpCodes.Ldelem_Ref);
                //load value for delegateParams[i].value to stack
                ilGen.Emit(OpCodes.Ldfld, param.ValueField);
                //set local variable to loaded value
                ilGen.Emit(OpCodes.Stloc, i);
            }
            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                var def   = param.paramDef;
                if (def.paramMode != ParamMode.Instance && def.paramMode != ParamMode.Result)
                {
                    if (def.paramMode == ParamMode.In)
                    {
                        //load local variable for call if it simple param
                        ilGen.Emit(OpCodes.Ldloc, i);
                    }
                    else
                    {
                        //load reference to local variable for call if it out or ref param
                        ilGen.Emit(OpCodes.Ldloca, i);
                    }
                }
            }
            ilGen.Emit(OpCodes.Newobj, constructorInfo);
            if (returnId >= 0)
            {
                //set result of code to local var
                ilGen.Emit(OpCodes.Stloc, returnId);
            }
            //return local vals to our array
            for (int i = 0; i <= delegateParams.Length - 1; i++)
            {
                var param = delegateParams[i];
                //load first argument of method to stack, in our sytuation it "delegateParams" array
                ilGen.Emit(OpCodes.Ldarg, 0);
                //load current array index to stack
                ilGen.Emit(OpCodes.Ldc_I4, i);
                //load reference for delegateParams[i] to stack
                ilGen.Emit(OpCodes.Ldelem_Ref);
                //load value for local val
                ilGen.Emit(OpCodes.Ldloc, i);
                //set local variable to loaded value
                ilGen.Emit(OpCodes.Stfld, param.ValueField);
            }
            ilGen.Emit(OpCodes.Ret);
            delegat        = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate));
            Delegates[key] = delegat;
        }
Ejemplo n.º 6
0
        private void CreateDelegates()
        {
            var key = ReflectedNodesHelper.GetGeneratedKey(fieldInfo);

            if (!GetDelegates.TryGetValue(key, out getDelegat) || getDelegat == null)
            {
                DynamicMethod dynamicMethod = new DynamicMethod(fieldInfo.Name + "_DynamicGet", null, DynParamTypes, typeof(JitFieldNode));
                ILGenerator   ilGen         = dynamicMethod.GetILGenerator();
                int           instanceId    = -1;
                int           returnId      = -1;
                int           localIds      = 0;
                int           instanceLocId = -1;
                int           returnLocId   = -1;
                for (int i = 0; i <= delegateParams.Length - 1; i++)
                {
                    var param = delegateParams[i];
                    var def   = param.paramDef;
                    if (def.paramMode == ParamMode.Instance)
                    {
                        instanceId = i;
                        ilGen.DeclareLocal(def.paramType);
                        instanceLocId = localIds;
                        localIds++;
                    }
                    if (def.paramMode == ParamMode.Result)
                    {
                        returnId = i;
                        ilGen.DeclareLocal(def.paramType);
                        returnLocId = localIds;
                        localIds++;
                    }
                }

                if (instanceId >= 0)
                {
                    //load first argument of method to stack, in our situation it "delegateParams" array
                    ilGen.Emit(OpCodes.Ldarg, 0);
                    //load current array index to stack
                    ilGen.Emit(OpCodes.Ldc_I4, instanceId);
                    //load reference for delegateParams[i] to stack
                    ilGen.Emit(OpCodes.Ldelem_Ref);
                    //load value for delegateParams[i].value to stack
                    ilGen.Emit(OpCodes.Ldfld, delegateParams[instanceId].ValueField);
                    //set local variable to loaded value
                    ilGen.Emit(OpCodes.Stloc, instanceLocId);
                    //load local var for get field value
                    ilGen.Emit(OpCodes.Ldloc, instanceLocId);
                    ilGen.Emit(OpCodes.Ldfld, fieldInfo);
                }
                else
                {
                    ilGen.Emit(OpCodes.Ldsfld, fieldInfo);
                }


                if (returnId >= 0)
                {
                    //Set local var to value of target field
                    ilGen.Emit(OpCodes.Stloc, returnLocId);
                    //load first argument of method to stack, in our situation it "delegateParams" array
                    ilGen.Emit(OpCodes.Ldarg, 0);
                    //load current array index to stack
                    ilGen.Emit(OpCodes.Ldc_I4, returnId);
                    //load reference for delegateParams[i] to stack
                    ilGen.Emit(OpCodes.Ldelem_Ref);
                    //load value for local val
                    ilGen.Emit(OpCodes.Ldloc, returnLocId);
                    //set local variable to loaded value
                    ilGen.Emit(OpCodes.Stfld, delegateParams[returnId].ValueField);
                }

                ilGen.Emit(OpCodes.Ret);

                getDelegat = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate));

                GetDelegates[key] = getDelegat;
            }
            if ((!SetDelegates.TryGetValue(key, out setDelegat) || setDelegat == null) && !fieldInfo.IsReadOnly())
            {
                DynamicMethod dynamicMethod = new DynamicMethod(fieldInfo.Name + "_DynamicSet", null, DynParamTypes, typeof(JitFieldNode));
                ILGenerator   ilGen         = dynamicMethod.GetILGenerator();
                int           instanceId    = -1;
                int           valueId       = -1;
                int           localIds      = 0;
                int           instanceLocId = -1;
                int           valueLocId    = -1;
                for (int i = 0; i <= delegateParams.Length - 1; i++)
                {
                    var param = delegateParams[i];
                    var def   = param.paramDef;
                    //declare local variables in method
                    if (def.paramMode == ParamMode.Instance)
                    {
                        instanceId = i;
                        ilGen.DeclareLocal(def.paramType);
                        instanceLocId = localIds;
                        localIds++;
                    }
                    if (def.paramMode == ParamMode.Result)
                    {
                        valueId = i;
                        ilGen.DeclareLocal(def.paramType);
                        valueLocId = localIds;
                        localIds++;
                    }
                }

                if (instanceId >= 0)
                {
                    //load first argument of method to stack, in our situation it "delegateParams" array
                    ilGen.Emit(OpCodes.Ldarg, 0);
                    //load current array index to stack
                    ilGen.Emit(OpCodes.Ldc_I4, instanceId);
                    //load reference for delegateParams[i] to stack
                    ilGen.Emit(OpCodes.Ldelem_Ref);
                    //load value for delegateParams[i].value to stack
                    ilGen.Emit(OpCodes.Ldfld, delegateParams[instanceId].ValueField);
                    //set local variable to loaded value
                    ilGen.Emit(OpCodes.Stloc, instanceLocId);
                }

                if (valueId >= 0)
                {
                    //load first argument of method to stack, in our situation it "delegateParams" array
                    ilGen.Emit(OpCodes.Ldarg, 0);
                    //load current array index to stack
                    ilGen.Emit(OpCodes.Ldc_I4, valueId);
                    //load reference for delegateParams[i] to stack
                    ilGen.Emit(OpCodes.Ldelem_Ref);
                    //load value for delegateParams[i].value to stack
                    ilGen.Emit(OpCodes.Ldfld, delegateParams[valueId].ValueField);
                    //set local variable to loaded value
                    ilGen.Emit(OpCodes.Stloc, valueLocId);
                    if (instanceId >= 0)
                    {
                        //set instance for set field value
                        ilGen.Emit(delegateParams[instanceId].GetCurrentType().RTIsValueType() ? OpCodes.Ldloca : OpCodes.Ldloc, instanceLocId);
                        //set new value for field
                        ilGen.Emit(OpCodes.Ldloc, valueLocId);
                        //set field value
                        ilGen.Emit(OpCodes.Stfld, fieldInfo);
                    }
                    else
                    {
                        //set new value for field
                        ilGen.Emit(OpCodes.Ldloc, valueLocId);
                        //set field value
                        ilGen.Emit(OpCodes.Stsfld, fieldInfo);
                    }
                }

                if (instanceId >= 0)
                {
                    //load first argument of method to stack, in our situation it "delegateParams" array
                    ilGen.Emit(OpCodes.Ldarg, 0);
                    //load current array index to stack
                    ilGen.Emit(OpCodes.Ldc_I4, instanceId);
                    //load reference for delegateParams[i] to stack
                    ilGen.Emit(OpCodes.Ldelem_Ref);
                    //load value for local val
                    ilGen.Emit(OpCodes.Ldloc, instanceLocId);
                    //set local variable to loaded value
                    ilGen.Emit(OpCodes.Stfld, delegateParams[instanceId].ValueField);
                }

                ilGen.Emit(OpCodes.Ret);

                setDelegat = (UniversalDelegate)dynamicMethod.CreateDelegate(typeof(UniversalDelegate));

                SetDelegates[key] = setDelegat;
            }
        }