我有一个 COM 对象的实例...它是这样创建的:
Type type = TypeDelegator.GetTypeFromProgID("Broker.Application");
Object application = Activator.CreateInstance(type);
当我尝试调用方法时:
type.GetMethod("RefreshAll").Invoke(application, null);
->
type.GetMethod("RefreshAll")
返回 null
。
当我尝试使用 type.GetMethods()
获取所有方法时,只有这些方法:
RefreshAll方法在哪里?我该如何调用它?
您不能在 COM 对象上使用 GetMethod,您必须使用不同的方法 方式:
this.application.GetType().InvokeMember("RefreshAll", BindingFlags.InvokeMethod, null, this.application, null);
我在一个使用 COM 的旧项目中使用这种方式,所以它应该适合你。
我意识到这是一个迟到的答案,但 c# 4 通过引入
dynamic
关键字(在设计时考虑了 COM 互操作)而改变了一些事情。
MSDN:
C# 团队在 C# 4 版本中专门针对的 COM 互操作场景是针对 Microsoft Office 应用程序(例如 Word 和 Excel)进行编程。目的是使 C# 中的此任务像在 Visual Basic 中一样简单、自然。 [1]
您的代码现在变成:
Type type = TypeDelegator.GetTypeFromProgID("Broker.Application");
dynamic application = Activator.CreateInstance(type);
application.RefreshAll(); // <---- new in c# 4
现在您将不会在 Visual Studio 语句完成中看到
RefreshAll()
,因此不必惊慌。它将编译。
虽然我将 _Profile 作为动态 com 对象,但我正在尝试使用此代码来访问 ElevationAt Methode,但它对我不起作用