EmitUnbox() static private method

static private EmitUnbox ( ILGenerator il, Type target_type, TypeCode target ) : void
il ILGenerator
target_type System.Type
target TypeCode
return void
Example #1
0
        private static FieldAccessor SpitAndInstantiateClassFor(FieldInfo field)
        {
            Type        ft = field.FieldType;
            TypeBuilder tb = FieldAccessor.module.DefineType("accessor" + FieldAccessor.count++, TypeAttributes.Public, typeof(FieldAccessor));

            MethodBuilder mb = tb.DefineMethod("GetValue", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot,
                                               Typeob.Object, new Type[] { Typeob.Object });
            ILGenerator il = mb.GetILGenerator();

            if (field.IsLiteral)
            {
                (new ConstantWrapper(field.GetValue(null), null)).TranslateToIL(il, ft);
            }
            else if (field.IsStatic)
            {
                il.Emit(OpCodes.Ldsfld, field);
            }
            else
            {
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldfld, field);
            }
            if (ft.IsValueType)
            {
                il.Emit(OpCodes.Box, ft);
            }
            il.Emit(OpCodes.Ret);


            mb = tb.DefineMethod("SetValue", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot,
                                 Typeob.Void, new Type[] { Typeob.Object, Typeob.Object });
            il = mb.GetILGenerator();
            if (!field.IsLiteral)
            {
                if (!field.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_1);
                }
                il.Emit(OpCodes.Ldarg_2);
                if (ft.IsValueType)
                {
                    Convert.EmitUnbox(il, ft, Type.GetTypeCode(ft));
                }
                if (field.IsStatic)
                {
                    il.Emit(OpCodes.Stsfld, field);
                }
                else
                {
                    il.Emit(OpCodes.Stfld, field);
                }
            }
            il.Emit(OpCodes.Ret);

            Type t = tb.CreateType();

            return((FieldAccessor)Activator.CreateInstance(t));
        }
Example #2
0
        private static MethodInvoker SpitAndInstantiateClassFor(MethodInfo method)
        {
            TypeBuilder   tb = MethodInvoker.module.DefineType("invoker" + MethodInvoker.count++, TypeAttributes.Public, typeof(MethodInvoker));
            MethodBuilder mb = tb.DefineMethod("Invoke", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot,
                                               typeof(Object), new Type[] { typeof(Object), typeof(Object[]) });

#if !DEBUG
            mb.SetCustomAttribute(new CustomAttributeBuilder(CompilerGlobals.debuggerStepThroughAttributeCtor, new Object[] {}));
            mb.SetCustomAttribute(new CustomAttributeBuilder(CompilerGlobals.debuggerHiddenAttributeCtor, new Object[] {}));
#endif
            ILGenerator il = mb.GetILGenerator();
            if (!method.DeclaringType.IsPublic)
            {
                method = method.GetBaseDefinition();
            }
            Type obT = method.DeclaringType;
            if (!method.IsStatic)
            {
                il.Emit(OpCodes.Ldarg_1);
                if (obT.IsValueType)
                {
                    Convert.EmitUnbox(il, obT, Type.GetTypeCode(obT));
                    Convert.EmitLdloca(il, obT);
                }
                else
                {
                    il.Emit(OpCodes.Castclass, obT);
                }
            }
            ParameterInfo[] parameters = method.GetParameters();
            LocalBuilder[]  outTemps   = null;
            for (int i = 0, n = parameters.Length; i < n; i++)
            {
                il.Emit(OpCodes.Ldarg_2);
                ConstantWrapper.TranslateToILInt(il, i);
                Type pt = parameters[i].ParameterType;
                if (pt.IsByRef)
                {
                    pt = pt.GetElementType();
                    if (outTemps == null)
                    {
                        outTemps = new LocalBuilder[n];
                    }
                    outTemps[i] = il.DeclareLocal(pt);
                    il.Emit(OpCodes.Ldelem_Ref);
                    if (pt.IsValueType)
                    {
                        Convert.EmitUnbox(il, pt, Type.GetTypeCode(pt));
                    }
                    il.Emit(OpCodes.Stloc, outTemps[i]);
                    il.Emit(OpCodes.Ldloca, outTemps[i]);
                }
                else
                {
                    il.Emit(OpCodes.Ldelem_Ref);
                    if (pt.IsValueType)
                    {
                        Convert.EmitUnbox(il, pt, Type.GetTypeCode(pt));
                    }
                }
            }
            if (!method.IsStatic && method.IsVirtual && !method.IsFinal && !obT.IsSealed)
            {
                il.Emit(OpCodes.Callvirt, method);
            }
            else
            {
                il.Emit(OpCodes.Call, method);
            }
            Type rt = method.ReturnType;
            if (rt == Typeob.Void)
            {
                il.Emit(OpCodes.Ldnull);
            }
            else if (rt.IsValueType)
            {
                il.Emit(OpCodes.Box, rt);
            }
            if (outTemps != null)
            {
                for (int i = 0, n = parameters.Length; i < n; i++)
                {
                    LocalBuilder loc = outTemps[i];
                    if (loc != null)
                    {
                        il.Emit(OpCodes.Ldarg_2);
                        ConstantWrapper.TranslateToILInt(il, i);
                        il.Emit(OpCodes.Ldloc, loc);
                        Type pt = parameters[i].ParameterType.GetElementType();
                        if (pt.IsValueType)
                        {
                            il.Emit(OpCodes.Box, pt);
                        }
                        il.Emit(OpCodes.Stelem_Ref);
                    }
                }
            }
            il.Emit(OpCodes.Ret);
            Type t = tb.CreateType();
            return((MethodInvoker)Activator.CreateInstance(t));
        }
Example #3
0
        private static FieldAccessor SpitAndInstantiateClassFor(FieldInfo field)
        {
            Type        ft = field.FieldType;
            TypeBuilder tb = Runtime.ThunkModuleBuilder.DefineType("accessor" + FieldAccessor.count++, TypeAttributes.Public, typeof(FieldAccessor));

            MethodBuilder mb = tb.DefineMethod("GetValue", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot,
                                               typeof(Object), new Type[] { typeof(Object) });

#if !DEBUG
            mb.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerStepThroughAttributeCtor, new Object[] {}));
            mb.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerHiddenAttributeCtor, new Object[] {}));
#endif
            ILGenerator il = mb.GetILGenerator();
            if (field.IsLiteral)
            {
                (new ConstantWrapper(TypeReferences.GetConstantValue(field), null)).TranslateToIL(il, ft);
            }
            else if (field.IsStatic)
            {
                il.Emit(OpCodes.Ldsfld, field);
            }
            else
            {
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldfld, field);
            }
            if (ft.IsValueType)
            {
                il.Emit(OpCodes.Box, ft);
            }
            il.Emit(OpCodes.Ret);


            mb = tb.DefineMethod("SetValue", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot,
                                 typeof(void), new Type[] { typeof(Object), typeof(Object) });
#if !DEBUG
            mb.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerStepThroughAttributeCtor, new Object[] {}));
            mb.SetCustomAttribute(new CustomAttributeBuilder(Runtime.TypeRefs.debuggerHiddenAttributeCtor, new Object[] {}));
#endif
            il = mb.GetILGenerator();
            if (!field.IsLiteral)
            {
                if (!field.IsStatic)
                {
                    il.Emit(OpCodes.Ldarg_1);
                }
                il.Emit(OpCodes.Ldarg_2);
                if (ft.IsValueType)
                {
                    Convert.EmitUnbox(il, ft, Type.GetTypeCode(ft));
                }
                if (field.IsStatic)
                {
                    il.Emit(OpCodes.Stsfld, field);
                }
                else
                {
                    il.Emit(OpCodes.Stfld, field);
                }
            }
            il.Emit(OpCodes.Ret);

            Type t = tb.CreateType();
            return((FieldAccessor)Activator.CreateInstance(t));
        }