Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PolicyFactory.cs
- PeerCustomResolverElement.cs
- PrimitiveCodeDomSerializer.cs
- WebMessageEncodingElement.cs
- ColorConverter.cs
- HttpApplicationFactory.cs
- ISCIIEncoding.cs
- ImageSourceConverter.cs
- WebServiceData.cs
- TableRow.cs
- CacheVirtualItemsEvent.cs
- PassportIdentity.cs
- GregorianCalendar.cs
- KeyValueConfigurationCollection.cs
- PropertyDescriptor.cs
- COM2ColorConverter.cs
- TextServicesPropertyRanges.cs
- keycontainerpermission.cs
- Point3D.cs
- FixedDocument.cs
- EntityDataSourceSelectedEventArgs.cs
- DesignerCommandSet.cs
- COM2TypeInfoProcessor.cs
- RelationshipWrapper.cs
- EntityWithChangeTrackerStrategy.cs
- StringStorage.cs
- ConnectionsZoneDesigner.cs
- StylusOverProperty.cs
- cookieexception.cs
- PeerIPHelper.cs
- CapacityStreamGeometryContext.cs
- ToolStripSettings.cs
- AutomationPatternInfo.cs
- FilterException.cs
- TextEditorMouse.cs
- NameValuePair.cs
- OdbcConnectionPoolProviderInfo.cs
- DataRecordObjectView.cs
- FileDataSourceCache.cs
- WindowsGraphicsCacheManager.cs
- LayoutDump.cs
- KnownColorTable.cs
- SqlInternalConnection.cs
- SingleKeyFrameCollection.cs
- GeometryCollection.cs
- AdjustableArrowCap.cs
- RotateTransform.cs
- CheckedListBox.cs
- DataGridColumn.cs
- JoinGraph.cs
- OpenTypeLayoutCache.cs
- RuleSetBrowserDialog.cs
- ColorPalette.cs
- ToolStripMenuItem.cs
- DockPatternIdentifiers.cs
- ControlBindingsCollection.cs
- UserNamePasswordValidator.cs
- DataGridViewCheckBoxCell.cs
- FrameworkContextData.cs
- QueryPageSettingsEventArgs.cs
- HostedBindingBehavior.cs
- TransactionProtocol.cs
- TextInfo.cs
- GridErrorDlg.cs
- WebColorConverter.cs
- BindingList.cs
- CodePageUtils.cs
- CroppedBitmap.cs
- WindowsServiceCredential.cs
- DataBinder.cs
- BooleanConverter.cs
- JoinGraph.cs
- UpdateTranslator.cs
- WebPartDescription.cs
- SimpleRecyclingCache.cs
- XmlSchemaAnnotated.cs
- StorageScalarPropertyMapping.cs
- WeakReadOnlyCollection.cs
- HostProtectionException.cs
- LazyTextWriterCreator.cs
- ActivityTrace.cs
- ISAPIWorkerRequest.cs
- ModulesEntry.cs
- HttpResponseWrapper.cs
- EventProviderWriter.cs
- LinkLabel.cs
- ObjectCacheHost.cs
- XmlSchemaExporter.cs
- DetailsViewCommandEventArgs.cs
- DistinctQueryOperator.cs
- XmlILAnnotation.cs
- AnnotationComponentChooser.cs
- DecimalAnimationUsingKeyFrames.cs
- SymbolEqualComparer.cs
- SerializationInfo.cs
- ObjectContextServiceProvider.cs
- InputBinder.cs
- HuffCodec.cs
- FloatSumAggregationOperator.cs
- SQLBinaryStorage.cs