从System .__ ComObject基类型调用方法

时间:2009-11-10 09:27:38

标签: .net reflection com-interop comobject

我正在尝试从msi文件中获取一些信息

我用过:

Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
object installerInstance = installerType.CreateInstance(installerType);

我很清楚添加对文件C:\ windows \ system32 \ msi.dll的引用的选项,并将installerInstance强制转换为WindowsInstaller.Install,但由于我的应用程序将在许多不同的操作系统上运行(xp, 2003,vista,7,2008)和处理器(x86 - x64),我想动态使用该实例。

问题是我无法访问底层的“WindowsInstaller.Installer”类型,只有System .__ ComObject方法可见且可执行。

如何从底层对象动态调用方法,例如“OpenDatabase”等?

1 个答案:

答案 0 :(得分:4)

您需要使用反射来调用方法。这是一个调用RunWindows Script Host方法的示例:

// 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" }
);