从系统.__ ComObject基类型调用方法
-
19-09-2019 - |
题
我想从一个msi文件的一些信息
我使用:
Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
object installerInstance = installerType.CreateInstance(installerType);
我很清楚的选项添加参考文件C:\ WINDOWS \ SYSTEM32 \ MSI.DLL,并投installerInstance到WindowsInstaller.Install,但由于我的应用程序会在许多不同的操作系统上运行(XP, 2003,远景,7,2008)和处理器(86 - 64),我要动态地使用该实例
问题是,我无法到达底层“WindowsInstaller.Installer”类型,只有系统.__ ComObject方法是可见的和可执行的。
如何动态地调用的方法,如“的openDatabase”等...从底层对象?
解决方案
您需要使用反射来调用方法。下面是一个例子调用运行的方法 Windows脚本宿主:
// obtain the COM type:
Type type = Type.GetTypeFromProgID("WScript.Shell");
// create an instance of the COM type
object instance = Activator.CreateInstance(type);
// Invoke the Run method on this instance by passing an argument
type.InvokeMember(
"Run",
BindingFlags.InvokeMethod,
null,
instance,
new[] { @"c:\windows\notepad.exe" }
);
不隶属于 StackOverflow