ArglessEventHandlerProxy.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / xsp / System / Web / Util / ArglessEventHandlerProxy.cs / 1 / ArglessEventHandlerProxy.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Web.Util { 
    using System; 
    using System.Reflection;
    using System.Reflection.Emit; 
    using System.Security.Permissions;

/*
 * Proxy that provides EventHandler and redirects it to arg-less method on the given object 
 */
    internal class ArglessEventHandlerProxy { 
        private Object _target; 
        private MethodInfo _arglessMethod;
 
        internal ArglessEventHandlerProxy(Object target, MethodInfo arglessMethod) {
            Debug.Assert(arglessMethod.GetParameters().Length == 0);

            _target = target; 
            _arglessMethod = arglessMethod;
        } 
 
        internal void Callback(Object sender, EventArgs e) {
            _arglessMethod.Invoke(_target, new Object[0]); 
        }

        internal EventHandler Handler {
            get { 
                return new EventHandler(Callback);
            } 
        } 
    }
 
    internal delegate void VoidMethod();

#if UNUSED_CODE
    internal class ArglessEventHandlerDelegateProxy { 
        private VoidMethod _vm;
 
        internal ArglessEventHandlerDelegateProxy(VoidMethod vm) { 
            _vm = vm;
        } 

        internal void Callback(Object sender, EventArgs e) {
            _vm();
        } 

        internal EventHandler Handler { 
            get { 
                return new EventHandler(Callback);
            } 
        }
    }
#endif //UNUSED_CODE
 
    internal class CalliEventHandlerDelegateProxy {
        private IntPtr _functionPointer; 
        private Object _target; 
        private bool _argless;
 
        internal CalliEventHandlerDelegateProxy(Object target, IntPtr functionPointer, bool argless) {
            _argless = argless;
            _target = target;
            _functionPointer = functionPointer; 
        }
 
        internal void Callback(Object sender, EventArgs e) { 
            if (_argless) {
                System.Web.Util.CalliHelper.ArglessFunctionCaller(_functionPointer, _target); 
            }
            else {
                System.Web.Util.CalliHelper.EventArgFunctionCaller(_functionPointer, _target, sender, e);
            } 
        }
 
        internal EventHandler Handler { 
            get {
                return new EventHandler(Callback); 
            }
        }
    }
 
#if LCG_Implementation
    internal delegate void ArglessMethod(IntPtr methodPtr, Object target); 
    internal delegate void EventArgMethod(IntPtr methodPtr, Object target, Object source, EventArgs e); 

    internal class CalliHelper { 
        internal static ArglessMethod ArglessFunctionCaller;
        internal static EventArgMethod EventArgFunctionCaller;

        // Make sure we have reflection permission to use ReflectionEmit and access method 
        [ReflectionPermission(SecurityAction.Assert, Flags = ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.MemberAccess)]
        static CalliHelper() { 
            // generate void(void) calli 
            DynamicMethod dm = new DynamicMethod("void_calli",
                                                    typeof(void), 
                                                    new Type[] { typeof(IntPtr) /* function ptr */, typeof(Object) /* target */},
                                                    typeof(CalliHelper).Module);
            ILGenerator ilg = dm.GetILGenerator();
 
            ilg.Emit(OpCodes.Ldarg_1);
            ilg.Emit(OpCodes.Ldarg_0); 
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[0], null); 
            ilg.Emit(OpCodes.Ret);
            ArglessFunctionCaller = (ArglessMethod)dm.CreateDelegate(typeof(ArglessMethod)); 

            // generate void(Object, EventArgs) calli
            dm = new DynamicMethod("eventarg_calli",
                                    typeof(void), 
                                    new Type[] { typeof(IntPtr) /* function ptr */, typeof(Object) /* target */, typeof(Object) /* sender */, typeof(EventArgs) },
                                    typeof(CalliHelper).Module); 
            ilg = dm.GetILGenerator(); 

            ilg.Emit(OpCodes.Ldarg_1); 
            ilg.Emit(OpCodes.Ldarg_2);
            ilg.Emit(OpCodes.Ldarg_3);
            ilg.Emit(OpCodes.Ldarg_0);
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[] { typeof(object) /* sender */, typeof(EventArgs) }, null); 
            ilg.Emit(OpCodes.Ret);
            EventArgFunctionCaller = (EventArgMethod)dm.CreateDelegate(typeof(EventArgMethod)); 
        } 
    }
#endif //LCG_Implementation 

#if  Reflection_Emit_Implementation
    internal delegate void ArglessMethod(IntPtr methodPtr, Object target);
    internal delegate void EventArgMethod(IntPtr methodPtr, Object target, Object source, EventArgs e); 

    internal class CalliHelper { 
 
        internal static ArglessMethod ArglessFunctionCaller;
        internal static EventArgMethod EventArgFunctionCaller; 

        // Make sure we have reflection permission to use ReflectionEmit and access method
        [ReflectionPermission(SecurityAction.Assert, Flags = ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.MemberAccess)]
        static CalliHelper() { 

            AssemblyName an = new AssemblyName("CalliHelper"); 
            AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); 
            ModuleBuilder mb = ab.DefineDynamicModule("CalliHelper.dll");
 
            ConstructorInfo ci = typeof(CLSCompliantAttribute).GetConstructor(new Type[] { typeof(bool) });
            CustomAttributeBuilder cb = new CustomAttributeBuilder(ci, new object[] { true });
            mb.SetCustomAttribute(cb);
 
            TypeBuilder tb = mb.DefineType("System.Web.Util.CalliHelper", TypeAttributes.NotPublic | TypeAttributes.Sealed);
            MethodBuilder methb = tb.DefineMethod("EventArgMethod", MethodAttributes.Assembly | MethodAttributes.Static, typeof(void), new Type[] { typeof(IntPtr), typeof(object), typeof(object), typeof(EventArgs) }); 
            ILGenerator ilg = methb.GetILGenerator(); 

            ilg.Emit(OpCodes.Ldarg_1); 
            ilg.Emit(OpCodes.Ldarg_2);
            ilg.Emit(OpCodes.Ldarg_3);
            ilg.Emit(OpCodes.Ldarg_0);
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[] { typeof(object), typeof(EventArgs) }, null); 
            ilg.Emit(OpCodes.Ret);
 
            methb = tb.DefineMethod("ArglessMethod", MethodAttributes.Assembly | MethodAttributes.Static, typeof(void), new Type[] { typeof (IntPtr), typeof(object) }); 
            ilg = methb.GetILGenerator();
 
            ilg.Emit(OpCodes.Ldarg_1);
            ilg.Emit(OpCodes.Ldarg_0);
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[0], null);
            ilg.Emit(OpCodes.Ret); 

            Type t = tb.CreateType(); 
            ArglessFunctionCaller = (ArglessMethod)Delegate.CreateDelegate(typeof(ArglessMethod), t, "ArglessMethod", true); 
            EventArgFunctionCaller = (EventArgMethod)Delegate.CreateDelegate(typeof(EventArgMethod), t, "EventArgMethod", true);
 
            //ab.Save("CalliHelper.dll");
        }
    }
#endif // Reflection_Emit_Implementation 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Web.Util { 
    using System; 
    using System.Reflection;
    using System.Reflection.Emit; 
    using System.Security.Permissions;

/*
 * Proxy that provides EventHandler and redirects it to arg-less method on the given object 
 */
    internal class ArglessEventHandlerProxy { 
        private Object _target; 
        private MethodInfo _arglessMethod;
 
        internal ArglessEventHandlerProxy(Object target, MethodInfo arglessMethod) {
            Debug.Assert(arglessMethod.GetParameters().Length == 0);

            _target = target; 
            _arglessMethod = arglessMethod;
        } 
 
        internal void Callback(Object sender, EventArgs e) {
            _arglessMethod.Invoke(_target, new Object[0]); 
        }

        internal EventHandler Handler {
            get { 
                return new EventHandler(Callback);
            } 
        } 
    }
 
    internal delegate void VoidMethod();

#if UNUSED_CODE
    internal class ArglessEventHandlerDelegateProxy { 
        private VoidMethod _vm;
 
        internal ArglessEventHandlerDelegateProxy(VoidMethod vm) { 
            _vm = vm;
        } 

        internal void Callback(Object sender, EventArgs e) {
            _vm();
        } 

        internal EventHandler Handler { 
            get { 
                return new EventHandler(Callback);
            } 
        }
    }
#endif //UNUSED_CODE
 
    internal class CalliEventHandlerDelegateProxy {
        private IntPtr _functionPointer; 
        private Object _target; 
        private bool _argless;
 
        internal CalliEventHandlerDelegateProxy(Object target, IntPtr functionPointer, bool argless) {
            _argless = argless;
            _target = target;
            _functionPointer = functionPointer; 
        }
 
        internal void Callback(Object sender, EventArgs e) { 
            if (_argless) {
                System.Web.Util.CalliHelper.ArglessFunctionCaller(_functionPointer, _target); 
            }
            else {
                System.Web.Util.CalliHelper.EventArgFunctionCaller(_functionPointer, _target, sender, e);
            } 
        }
 
        internal EventHandler Handler { 
            get {
                return new EventHandler(Callback); 
            }
        }
    }
 
#if LCG_Implementation
    internal delegate void ArglessMethod(IntPtr methodPtr, Object target); 
    internal delegate void EventArgMethod(IntPtr methodPtr, Object target, Object source, EventArgs e); 

    internal class CalliHelper { 
        internal static ArglessMethod ArglessFunctionCaller;
        internal static EventArgMethod EventArgFunctionCaller;

        // Make sure we have reflection permission to use ReflectionEmit and access method 
        [ReflectionPermission(SecurityAction.Assert, Flags = ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.MemberAccess)]
        static CalliHelper() { 
            // generate void(void) calli 
            DynamicMethod dm = new DynamicMethod("void_calli",
                                                    typeof(void), 
                                                    new Type[] { typeof(IntPtr) /* function ptr */, typeof(Object) /* target */},
                                                    typeof(CalliHelper).Module);
            ILGenerator ilg = dm.GetILGenerator();
 
            ilg.Emit(OpCodes.Ldarg_1);
            ilg.Emit(OpCodes.Ldarg_0); 
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[0], null); 
            ilg.Emit(OpCodes.Ret);
            ArglessFunctionCaller = (ArglessMethod)dm.CreateDelegate(typeof(ArglessMethod)); 

            // generate void(Object, EventArgs) calli
            dm = new DynamicMethod("eventarg_calli",
                                    typeof(void), 
                                    new Type[] { typeof(IntPtr) /* function ptr */, typeof(Object) /* target */, typeof(Object) /* sender */, typeof(EventArgs) },
                                    typeof(CalliHelper).Module); 
            ilg = dm.GetILGenerator(); 

            ilg.Emit(OpCodes.Ldarg_1); 
            ilg.Emit(OpCodes.Ldarg_2);
            ilg.Emit(OpCodes.Ldarg_3);
            ilg.Emit(OpCodes.Ldarg_0);
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[] { typeof(object) /* sender */, typeof(EventArgs) }, null); 
            ilg.Emit(OpCodes.Ret);
            EventArgFunctionCaller = (EventArgMethod)dm.CreateDelegate(typeof(EventArgMethod)); 
        } 
    }
#endif //LCG_Implementation 

#if  Reflection_Emit_Implementation
    internal delegate void ArglessMethod(IntPtr methodPtr, Object target);
    internal delegate void EventArgMethod(IntPtr methodPtr, Object target, Object source, EventArgs e); 

    internal class CalliHelper { 
 
        internal static ArglessMethod ArglessFunctionCaller;
        internal static EventArgMethod EventArgFunctionCaller; 

        // Make sure we have reflection permission to use ReflectionEmit and access method
        [ReflectionPermission(SecurityAction.Assert, Flags = ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.MemberAccess)]
        static CalliHelper() { 

            AssemblyName an = new AssemblyName("CalliHelper"); 
            AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); 
            ModuleBuilder mb = ab.DefineDynamicModule("CalliHelper.dll");
 
            ConstructorInfo ci = typeof(CLSCompliantAttribute).GetConstructor(new Type[] { typeof(bool) });
            CustomAttributeBuilder cb = new CustomAttributeBuilder(ci, new object[] { true });
            mb.SetCustomAttribute(cb);
 
            TypeBuilder tb = mb.DefineType("System.Web.Util.CalliHelper", TypeAttributes.NotPublic | TypeAttributes.Sealed);
            MethodBuilder methb = tb.DefineMethod("EventArgMethod", MethodAttributes.Assembly | MethodAttributes.Static, typeof(void), new Type[] { typeof(IntPtr), typeof(object), typeof(object), typeof(EventArgs) }); 
            ILGenerator ilg = methb.GetILGenerator(); 

            ilg.Emit(OpCodes.Ldarg_1); 
            ilg.Emit(OpCodes.Ldarg_2);
            ilg.Emit(OpCodes.Ldarg_3);
            ilg.Emit(OpCodes.Ldarg_0);
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[] { typeof(object), typeof(EventArgs) }, null); 
            ilg.Emit(OpCodes.Ret);
 
            methb = tb.DefineMethod("ArglessMethod", MethodAttributes.Assembly | MethodAttributes.Static, typeof(void), new Type[] { typeof (IntPtr), typeof(object) }); 
            ilg = methb.GetILGenerator();
 
            ilg.Emit(OpCodes.Ldarg_1);
            ilg.Emit(OpCodes.Ldarg_0);
            ilg.EmitCalli(OpCodes.Calli, CallingConventions.HasThis, typeof(void), new Type[0], null);
            ilg.Emit(OpCodes.Ret); 

            Type t = tb.CreateType(); 
            ArglessFunctionCaller = (ArglessMethod)Delegate.CreateDelegate(typeof(ArglessMethod), t, "ArglessMethod", true); 
            EventArgFunctionCaller = (EventArgMethod)Delegate.CreateDelegate(typeof(EventArgMethod), t, "EventArgMethod", true);
 
            //ab.Save("CalliHelper.dll");
        }
    }
#endif // Reflection_Emit_Implementation 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK