zoukankan      html  css  js  c++  java
  • 反射事件参数

    namespace ConsoleApplication
    {
        class Program
        {
            public static  void s_TestEvent(string msg)
            {
                Console.WriteLine(msg);
            }

            static void Main(string[] args)
            {
                Assembly ass = Assembly.Load("ConsoleApplication");

                object structInstance = ass.CreateInstance("ConsoleApplication.PrivateField");

                Type structType = structInstance.GetType();


                EventInfo eventInfo = structType.GetEvent("TestEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);

                Type tDelegate = eventInfo.EventHandlerType;

                MethodInfo miHandler = typeof(Program).GetMethod("s_TestEvent", BindingFlags.Public  | BindingFlags.Static);



                Delegate d = Delegate.CreateDelegate(tDelegate, miHandler);

                MethodInfo miAddHandler = eventInfo.GetAddMethod();

                object[] addHandlerArgs = { d };

                miAddHandler.Invoke(structInstance, addHandlerArgs);

                FieldInfo _Field = structType.GetField("TestEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);

                if (_Field != null)
                {
                    object _FieldValue = _Field.GetValue(structInstance);

                    if (_FieldValue != null && _FieldValue is Delegate)
                    {
                        Delegate _ObjectDelegate = _FieldValue as Delegate;       

                        _ObjectDelegate.DynamicInvoke(new object[] {"Sandy" });
                    }
                }
            }
        }
    }        

  • 相关阅读:
    Keras学习率调整
    机器学习算法的调试---梯度检验(Gradient Checking)
    Python 上下文管理器
    Python垃圾回收机制
    Css 动画的回调
    全新的membership框架Asp.net Identity——绕不过的Claims
    CSS代码重构与优化
    html5 本地存储
    ASP.NET MVC 随想录
    谈谈Angular关于$watch,$apply 以及 $digest的工作原理
  • 原文地址:https://www.cnblogs.com/dlbird/p/3975024.html
Copyright © 2011-2022 走看看