protected internal override Delegate CreateDelegate()
        {
            MethodInfo methodInfo      = LookupUtils.GetMethod(CallInfo);
            byte       paramArrayIndex = CallInfo.IsStatic ? (byte)0 : (byte)1;
            bool       hasReturnType   = methodInfo.ReturnType != Constants.VoidType;

            byte startUsableLocalIndex = 0;

            if (CallInfo.HasRefParam)
            {
                startUsableLocalIndex = CreateLocalsForByRefParams(paramArrayIndex, methodInfo);
                // create by_ref_locals from argument array
                Generator.DeclareLocal(hasReturnType ? methodInfo.ReturnType : Constants.ObjectType); // T result;
                GenerateInvocation(methodInfo, paramArrayIndex, (byte)(startUsableLocalIndex + 1));
                if (hasReturnType)
                {
                    Generator.stloc(startUsableLocalIndex); // result = <stack>;
                }
                AssignByRefParamsToArray(paramArrayIndex);  // store by_ref_locals back to argument array
            }
            else
            {
                Generator.DeclareLocal(hasReturnType ? methodInfo.ReturnType : Constants.ObjectType); // T result;
                GenerateInvocation(methodInfo, paramArrayIndex, (byte)(startUsableLocalIndex + 1));
                if (hasReturnType)
                {
                    Generator.stloc(startUsableLocalIndex); // result = <stack>;
                }
            }

            if (CallInfo.ShouldHandleInnerStruct)
            {
                StoreLocalToInnerStruct((byte)(startUsableLocalIndex + 1)); // ((ValueTypeHolder)this)).Value = tmpStr;
            }
            if (hasReturnType)
            {
                Generator.ldloc(startUsableLocalIndex)  // push result;
                .boxIfValueType(methodInfo.ReturnType); // box result;
            }
            else
            {
                Generator.ldnull.end(); // load null
            }

            Generator.ret();

            return(Method.CreateDelegate(CallInfo.IsStatic ? typeof(StaticMethodInvoker) : typeof(MethodInvoker)));
        }
        protected internal override Delegate CreateDelegate()
        {
            MemberInfo member            = LookupUtils.GetMember(CallInfo);
            bool       handleInnerStruct = CallInfo.ShouldHandleInnerStruct;

            if (handleInnerStruct)
            {
                Generator
                .ldarg_0                                        // load arg-0 (this)
                .DeclareLocal(CallInfo.TargetType);             // TargetType tmpStr
                LoadInnerStructToLocal(0);                      // tmpStr = ((ValueTypeHolder)this)).Value
                Generator.DeclareLocal(Constants.ObjectType);   // object result;
            }
            else if (!CallInfo.IsStatic)
            {
                Generator
                .ldarg_0                                  // load arg-0 (this)
                .castclass(CallInfo.TargetType);          // (TargetType)this
            }

            if (member.MemberType == MemberTypes.Field)
            {
                var field = member as FieldInfo;
                Generator
                .ldfld(CallInfo.IsStatic, field)            // (this|tmpStr).field OR TargetType.field
                .boxIfValueType(field.FieldType);           // (object)<stack>
            }
            else
            {
                PropertyInfo prop      = member as PropertyInfo;
                MethodInfo   getMethod = LookupUtils.GetPropertyGetMethod(prop, CallInfo);
                Generator
                .call(CallInfo.IsStatic || CallInfo.IsTargetTypeStruct, getMethod)       // (this|tmpStr).prop OR TargetType.prop
                .boxIfValueType(prop.PropertyType);                                      // (object)<stack>
            }

            if (handleInnerStruct)
            {
                Generator.stloc_1.end();           // resultLocal = <stack>
                StoreLocalToInnerStruct(0);        // ((ValueTypeHolder)this)).Value = tmpStr
                Generator.ldloc_1.end();           // push resultLocal
            }

            Generator.ret();

            return(CallInfo.IsStatic ? Method.CreateDelegate(typeof(StaticMemberGetter)) : Method.CreateDelegate(typeof(MemberGetter)));
        }
Exemple #3
0
        protected internal override Delegate CreateDelegate()
        {
            if (CallInfo.IsTargetTypeStruct && CallInfo.HasNoParam) // no-arg struct needs special initialization
            {
                Generator.DeclareLocal(CallInfo.TargetType);        // TargetType tmp
                Generator.ldloca_s(0)                               // &tmp
                .initobj(CallInfo.TargetType)                       // init_obj(&tmp)
                .ldloc_0                                            // load tmp
                .end();
            }
            else if (CallInfo.TargetType.IsArray)
            {
                Generator.ldarg_0                                    // load args[] (method arguments)
                .ldc_i4_0                                            // load 0
                .ldelem_ref                                          // load args[0] (length)
                .unbox_any(typeof(int))                              // unbox stack
                .newarr(CallInfo.TargetType.GetElementType());       // new T[args[0]]
            }
            else
            {
                ConstructorInfo ctorInfo = LookupUtils.GetConstructor(CallInfo);
                byte            startUsableLocalIndex = 0;
                if (CallInfo.HasRefParam)
                {
                    startUsableLocalIndex = CreateLocalsForByRefParams(0, ctorInfo); // create by_ref_locals from argument array
                    Generator.DeclareLocal(CallInfo.TargetType);                     // TargetType tmp;
                }

                PushParamsOrLocalsToStack(0);               // push arguments and by_ref_locals
                Generator.newobj(ctorInfo);                 // ctor (<stack>)

                if (CallInfo.HasRefParam)
                {
                    Generator.stloc(startUsableLocalIndex); // tmp = <stack>;
                    AssignByRefParamsToArray(0);            // store by_ref_locals back to argument array
                    Generator.ldloc(startUsableLocalIndex); // tmp
                }
            }
            Generator.boxIfValueType(CallInfo.TargetType)
            .ret();                                    // return (box)<stack>;

            return(Method.CreateDelegate(typeof(ConstructorInvoker)));
        }
Exemple #4
0
        protected internal override Delegate CreateDelegate()
        {
            MemberInfo member            = LookupUtils.GetMember(CallInfo);
            bool       handleInnerStruct = CallInfo.ShouldHandleInnerStruct;

            Generator.ldarg_0.end();                            // load arg-0 (this or value-to-be-set)
            if (handleInnerStruct)
            {
                Generator.DeclareLocal(CallInfo.TargetType);    // TargetType tmpStr
                LoadInnerStructToLocal(0);                      // tmpStr = ((ValueTypeHolder)this)).Value;
                Generator.ldarg_1.end();                        // load value-to-be-set;
            }
            else if (!CallInfo.IsStatic)
            {
                Generator.castclass(CallInfo.TargetType)      // (TargetType)this
                .ldarg_1.end();                               // load value-to-be-set;
            }

            Type memberType = member is FieldInfo ? ((FieldInfo)member).FieldType : ((PropertyInfo)member).PropertyType;

            Generator.CastFromObject(memberType);               // unbox | cast value-to-be-set
            if (member.MemberType == MemberTypes.Field)
            {
                Generator.stfld(CallInfo.IsStatic, (FieldInfo)member);  // (this|tmpStr).field = value-to-be-set;
            }
            else
            {
                MethodInfo setMethod = LookupUtils.GetPropertySetMethod((PropertyInfo)member, CallInfo);
                Generator.call(CallInfo.IsStatic || CallInfo.IsTargetTypeStruct, setMethod); // (this|tmpStr).set_Prop(value-to-be-set);
            }

            if (handleInnerStruct)
            {
                StoreLocalToInnerStruct(0); // ((ValueTypeHolder)this)).Value = tmpStr
            }

            Generator.ret();

            return(CallInfo.IsStatic ? Method.CreateDelegate(typeof(StaticMemberSetter)) : Method.CreateDelegate(typeof(MemberSetter)));
        }