經過反射綁定事件_Office Visio

花了好幾個小時才Try出來,記錄一下:this

 

            //反射獲取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);//訂閱事件事件

相關文章
相關標籤/搜索