【问题标题】:Invoking method from the System.__ComObject base type从 System.__ComObject 基类型调用方法
【发布时间】:2009-11-10 17:52:43
【问题描述】:

我正在尝试从 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”等?

【问题讨论】:

    标签: .net reflection com-interop comobject


    【解决方案1】:

    您需要使用反射来调用方法。下面是一个调用Windows Script HostRun方法的例子:

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

    【讨论】:

    • 谢谢。我试过这个,但我不小心把“this”作为实例传递了……谢谢你启发我
    猜你喜欢
    • 2016-02-02
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多