private void Form1_Load(object sender, EventArgs e) { object ppvComObject = null; try { Guid CLSID_ShellWindows = new Guid("9BA05972-F6A8-11CF-A442-00A0C90A8F39"); Type pComType = Type.GetTypeFromCLSID(CLSID_ShellWindows, true); ppvComObject = Activator.CreateInstance(pComType); int webCount; if ((webCount = (int)pComType.InvokeMember("Count", BindingFlags.GetProperty, null, ppvComObject, null)) > 0) { for (int i = 0; i < webCount; i++) { object webObject = pComType.InvokeMember("Item", // IWebBrowser BindingFlags.InvokeMethod, null, ppvComObject, new object[] { i }); webObject.GetType().InvokeMember("Navigate", BindingFlags.InvokeMethod, null, webObject, new object[] { "http://blog.csdn.net/u012395622" }); } } } finally { if (ppvComObject != null && Marshal.IsComObject(ppvComObject)) Marshal.ReleaseComObject(ppvComObject); } }
首先Type.GetTypeFromCLSID获取到COM对象的NET包装类型
然后再通过Activator.CreateInstance创建它的对象包装实例(System.__ComObject)
然后我们在通过Type.InvokeMember去调用它的方法/属性 整体是比较简单的
实际上这只是CLR中一种较为简单的反射机制的应用,但不能调用未注册的类型库。