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
- TextSegment.cs
- PartialToken.cs
- PassportAuthentication.cs
- SmiEventSink.cs
- DrawListViewColumnHeaderEventArgs.cs
- HybridCollection.cs
- CompilerResults.cs
- Int64KeyFrameCollection.cs
- ExpressionsCollectionConverter.cs
- GenericUriParser.cs
- RadialGradientBrush.cs
- SoapSchemaExporter.cs
- PTUtility.cs
- AggregateNode.cs
- EventArgs.cs
- DataGridHeadersVisibilityToVisibilityConverter.cs
- WaitForChangedResult.cs
- CheckBox.cs
- OutputBuffer.cs
- SqlCommandSet.cs
- SystemColors.cs
- CorrelationToken.cs
- DetailsViewUpdateEventArgs.cs
- ColumnHeaderConverter.cs
- RegexWriter.cs
- CalendarDay.cs
- ConfigXmlCDataSection.cs
- AddInContractAttribute.cs
- CompositeScriptReferenceEventArgs.cs
- PolicyStatement.cs
- ValidatingReaderNodeData.cs
- SerialPort.cs
- Vector3D.cs
- EventLogEntryCollection.cs
- SafeHandle.cs
- BitmapEffectOutputConnector.cs
- DelegateTypeInfo.cs
- TdsValueSetter.cs
- ArgumentNullException.cs
- EntityContainerAssociationSetEnd.cs
- XmlSchemaInferenceException.cs
- Policy.cs
- EntityModelBuildProvider.cs
- SoapClientProtocol.cs
- Point3DCollectionValueSerializer.cs
- XmlHierarchicalEnumerable.cs
- PeerEndPoint.cs
- FileChangeNotifier.cs
- CompressionTracing.cs
- HttpWebRequestElement.cs
- odbcmetadatacollectionnames.cs
- Range.cs
- Type.cs
- AsnEncodedData.cs
- EntityClientCacheKey.cs
- ProviderConnectionPoint.cs
- wmiprovider.cs
- DelayLoadType.cs
- TextDecorationCollection.cs
- EntityDataSourceChangedEventArgs.cs
- CodeDomSerializerException.cs
- InternalControlCollection.cs
- ACL.cs
- CustomAttributeFormatException.cs
- DesignerAutoFormatCollection.cs
- WindowsImpersonationContext.cs
- ScrollProviderWrapper.cs
- PointAnimationBase.cs
- PageEventArgs.cs
- UseLicense.cs
- UpdateProgress.cs
- EntityDataSourceContextCreatedEventArgs.cs
- OverrideMode.cs
- ExtensibleClassFactory.cs
- MessageEventSubscriptionService.cs
- Formatter.cs
- ProtectedConfigurationSection.cs
- PartialCachingAttribute.cs
- GridViewActionList.cs
- Command.cs
- MachineSettingsSection.cs
- WmlValidationSummaryAdapter.cs
- NameValueCollection.cs
- QueryCursorEventArgs.cs
- CustomWebEventKey.cs
- TextFormatterContext.cs
- RegexCapture.cs
- SemaphoreSecurity.cs
- ApplicationSecurityManager.cs
- StateManagedCollection.cs
- GroupBoxAutomationPeer.cs
- PenContext.cs
- Symbol.cs
- NamespaceInfo.cs
- FrameworkContentElement.cs
- AssertSection.cs
- StrongName.cs
- GeneratedCodeAttribute.cs
- SvcMapFile.cs
- IdnMapping.cs