通过反射绑定事件_Office Visio

花了好几个小时才Try出来,记录一下:

//反射获取Visio.Application,此处没有判断是否有安装Visio
            mVisioType = System.Type.GetTypeFromProgID("Visio.Application");
            object oVisioApplication = System.Activator.CreateInstance(mVisioType);//打开Visio
            //反射获取Visio Application的Documents对象
            object oDocuments = mVisioType.InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty, null, oVisioApplication, null);
            //事件
            //获取事件的下定义
            EventInfo documentOpenedEventInfo = mVisioType.GetEvent("DocumentOpened");
            MethodInfo documentOpenedMethod = this.GetType().GetMethod("DocumentOpened");//要为public,否则null

//GetMethod("DocumentOpened")中的DocumentOpened为此类的一个方法,要为public,为private反射会null
            Delegate documentOpenedHandler = Delegate.CreateDelegate(documentOpenedEventInfo.EventHandlerType, null, documentOpenedMethod);
            documentOpenedEventInfo.AddEventHandler(oVisioApplication, documentOpenedHandler);//订阅事件

上一篇:16个PHP设计模式详解


下一篇:position:sticky用法