コード例 #1
0
        private static GetControlsDelegate GetGetControlsDelegate()
        {
            GetControlsDelegate handler  = null;
            FieldInfo           controls = GetField("_controls");

            SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), delegate
            {
                System.Reflection.Emit.DynamicMethod dm = new System.Reflection.Emit.DynamicMethod("get_Controls", typeof(ControlCollection), new Type[] { typeof(Control) }, typeof(Control).Module, true);
                ILGenerator il = dm.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldfld, controls);
                il.Emit(OpCodes.Ret);
                handler = (GetControlsDelegate)dm.CreateDelegate(typeof(GetControlsDelegate));
            });
            return(handler);
        }
コード例 #2
0
        private static GetControlsDelegate GetGetControlsDelegate()
        {
            GetControlsDelegate handler = null;

            if (SystemUtils.Clr4Runtime)
            {
                FieldInfo controls = GetField("_controls");

                SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), delegate
                {
                    System.Reflection.Emit.DynamicMethod dm = new System.Reflection.Emit.DynamicMethod("get_Controls", typeof(ControlCollection), new Type[] { typeof(Control) }, typeof(Control).Module, true);
                    ILGenerator il = dm.GetILGenerator();
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldfld, controls);
                    il.Emit(OpCodes.Ret);
                    handler = (GetControlsDelegate)dm.CreateDelegate(typeof(GetControlsDelegate));
                });
            }
            else
            {
                FieldInfo occasionalFields = GetField("_occasionalFields");
                MethodInfo ensureOccasionalFields = GetMethod("EnsureOccasionalFields");
                FieldInfo controls = occasionalFields.FieldType.GetField("Controls");

                SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), delegate
                {
                    System.Reflection.Emit.DynamicMethod dm = new System.Reflection.Emit.DynamicMethod("get_Controls", typeof(ControlCollection), new Type[] { typeof(Control) }, typeof(Control).Module, true);
                    ILGenerator il = dm.GetILGenerator();
                    Label occFieldsNull = il.DefineLabel();
                    Label retControls = il.DefineLabel();
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldfld, occasionalFields);
                    il.Emit(OpCodes.Brfalse_S, occFieldsNull);
                    il.MarkLabel(retControls);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldfld, occasionalFields);
                    il.Emit(OpCodes.Ldfld, controls);
                    il.Emit(OpCodes.Ret);
                    il.MarkLabel(occFieldsNull);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Call, ensureOccasionalFields);
                    il.Emit(OpCodes.Br, retControls);
                    handler = (GetControlsDelegate)dm.CreateDelegate(typeof(GetControlsDelegate));
                });
            }
            return handler;
        }