Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Util / ArglessEventHandlerProxy.cs / 1305376 / 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; using System.Security; /* * 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; } [ReflectionPermission(SecurityAction.Assert, Flags=ReflectionPermissionFlag.RestrictedMemberAccess)] 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(); 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
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PropertyRecord.cs
- PersonalizationDictionary.cs
- DefaultCommandExtensionCallback.cs
- HttpCapabilitiesBase.cs
- FontResourceCache.cs
- XmlObjectSerializerReadContext.cs
- DataGridItem.cs
- IListConverters.cs
- FormViewPageEventArgs.cs
- ToolStripTemplateNode.cs
- CodeIterationStatement.cs
- Border.cs
- versioninfo.cs
- BuildProviderAppliesToAttribute.cs
- HtmlButton.cs
- SmuggledIUnknown.cs
- ProgressBarAutomationPeer.cs
- SpeakProgressEventArgs.cs
- SqlDataSourceSummaryPanel.cs
- VirtualDirectoryMappingCollection.cs
- RangeValuePattern.cs
- SpecularMaterial.cs
- XPathNodeList.cs
- ZoneIdentityPermission.cs
- EnumerationRangeValidationUtil.cs
- ToolStripDesignerAvailabilityAttribute.cs
- UdpSocket.cs
- UpdatePanelTrigger.cs
- input.cs
- CompositionDesigner.cs
- TdsParserHelperClasses.cs
- StylusOverProperty.cs
- EnumValAlphaComparer.cs
- Matrix3DConverter.cs
- Substitution.cs
- SecurityDescriptor.cs
- LocationReference.cs
- DataSourceXmlTextReader.cs
- WebPartDisplayModeCollection.cs
- RemoteTokenFactory.cs
- DataGridViewColumnDesigner.cs
- ScaleTransform.cs
- QuaternionAnimation.cs
- HttpServerVarsCollection.cs
- PropertyGridEditorPart.cs
- ValidationErrorCollection.cs
- RemoteX509AsymmetricSecurityKey.cs
- XamlGridLengthSerializer.cs
- PrtTicket_Public_Simple.cs
- LifetimeManager.cs
- MdiWindowListStrip.cs
- __FastResourceComparer.cs
- CodeMethodInvokeExpression.cs
- CellRelation.cs
- HealthMonitoringSection.cs
- CodeFieldReferenceExpression.cs
- TextServicesContext.cs
- ListViewGroupItemCollection.cs
- ResourceDisplayNameAttribute.cs
- WrappingXamlSchemaContext.cs
- BindToObject.cs
- ColorPalette.cs
- SQLDoubleStorage.cs
- HtmlSelect.cs
- AuditLevel.cs
- WorkflowHostingEndpoint.cs
- ToolStripKeyboardHandlingService.cs
- SqlClientWrapperSmiStreamChars.cs
- InvalidEnumArgumentException.cs
- ReservationCollection.cs
- ViewStateException.cs
- StructuredProperty.cs
- BitmapMetadata.cs
- SQLDateTime.cs
- Misc.cs
- StringFunctions.cs
- CollectionsUtil.cs
- DialogResultConverter.cs
- FileLogRecordHeader.cs
- Ref.cs
- DiscoveryReference.cs
- ImplicitInputBrush.cs
- DSACryptoServiceProvider.cs
- ToggleButtonAutomationPeer.cs
- SizeAnimationUsingKeyFrames.cs
- SettingsPropertyIsReadOnlyException.cs
- FlowLayoutPanelDesigner.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- WinEventHandler.cs
- FontSizeConverter.cs
- LogWriteRestartAreaState.cs
- OrderablePartitioner.cs
- TabPage.cs
- Menu.cs
- ValidationHelper.cs
- CombinedGeometry.cs
- SerializationObjectManager.cs
- GraphicsPath.cs
- XmlSchemaInferenceException.cs
- SrgsSemanticInterpretationTag.cs