当前位置: 首页>>代码示例>>C#>>正文


C# EventHandler.GetType方法代码示例

本文整理汇总了C#中EventHandler.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# EventHandler.GetType方法的具体用法?C# EventHandler.GetType怎么用?C# EventHandler.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在EventHandler的用法示例。


在下文中一共展示了EventHandler.GetType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LispDelegate

            public LispDelegate(LispEventProducer prod, object target, EventInfo method)
                //: base(target, method.Name)
            {
                Target = target;
                Producer = prod;
                Method = method;
                Handler = Invoke;

                Type t = target.GetType();
                // In order to create a method to handle the Elapsed event,
                // it is necessary to know the signature of the delegate 
                // used to raise the event. Reflection.Emit can then be
                // used to construct a dynamic class with a static method
                // that has the correct signature.

                // Get the event handler type of the Elapsed event. This is
                // a delegate type, so it has an Invoke method that has
                // the same signature as the delegate. The following code
                // creates an array of Type objects that represent the 
                // parameter types of the Invoke method.
                //    
                Type handlerType = method.EventHandlerType;
                MethodInfo invokeMethod = handlerType.GetMethod("Invoke");
                ParameterInfo[] parms = invokeMethod.GetParameters();
                Type[] parmTypes = new Type[parms.Length];
                for (int i = 0; i < parms.Length; i++)
                {
                    parmTypes[i] = parms[i].ParameterType;
                }
                if (false)
                {
                    // Use Reflection.Emit to create a dynamic assembly that
                    // will be run but not saved. An assembly must have at 
                    // least one module, which in this case contains a single
                    // type. The only purpose of this type is to contain the 
                    // event handler method. (In the .NET Framework version 
                    // 2.0 you can use dynamic methods, which are simpler 
                    // because there is no need to create an assembly, module,
                    // or type.)
                    AssemblyName aName = new AssemblyName();
                    aName.Name = "DynamicTypes";
                    AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
                    ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
                    TypeBuilder tb = mb.DefineType("Handler", TypeAttributes.Class | TypeAttributes.Public);
                    var localHandler = this.GetType().GetMethod("Invoke");

                    ConstructorBuilder Ctor = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard,
                                                                   null);
                    {
                        ILGenerator ilc = Ctor.GetILGenerator();
                        ilc.Emit(OpCodes.Ldarg_0);
                        ilc.Emit(OpCodes.Call, typeof (object).GetConstructor(new Type[0]));
                        ilc.Emit(OpCodes.Ret);
                    }
                    // Create the method that will handle the event. The name
                    // is not important. The method is static, because there is
                    // no reason to create an instance of the dynamic type.
                    //
                    // The parameter types and return type of the method are
                    // the same as those of the delegate's Invoke method, 
                    // captured earlier.
                    MethodBuilder handler = tb.DefineMethod("DynamicHandler",
                                                            MethodAttributes.Public | MethodAttributes.Static,
                                                            invokeMethod.ReturnType, parmTypes);

                    // Generate code to handle the event.
                    //
                    ILGenerator il = handler.GetILGenerator();
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldarg_1);
                    il.EmitCall(OpCodes.Call, localHandler, new Type[] {typeof (object), typeof (EventArgs)});
                    il.Emit(OpCodes.Ret);

                    // CreateType must be called before the Handler type can
                    // be used. In order to create the delegate that will
                    // handle the event, a MethodInfo from the finished type
                    // is required.
                    Type finished = tb.CreateType();
                    eventHandler = finished.GetMethod("DynamicHandler");
                }
                if (true)
                {
                    handlerType = Handler.GetType();
                    eventHandler = this.GetType().GetMethod("Invoke");
                }
                // Use the MethodInfo to create a delegate of the correct 
                // type, and call the AddEventHandler method to hook up 
                // the event.
                Delegate d = Delegate.CreateDelegate(handlerType, eventHandler);
                //typeof(object)
                //method.EventHandlerType.
                
                
                //method.AddEventHandler(target, d);

                // Late-bound calls to the Interval and Enabled property 
                // are required to enable the timer with a one-second
                // interval.
                //t.InvokeMember("Interval", BindingFlags.SetProperty, null, timer, new Object[] { 1000 });
                //t.InvokeMember("Enabled", BindingFlags.SetProperty, null, timer, new Object[] { true });
//.........这里部分代码省略.........
开发者ID:drzo,项目名称:opensim4opencog,代码行数:101,代码来源:LispEventProducer.cs


注:本文中的EventHandler.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。