我正在尝试通过 C# 访问 COM 对象(CST Studio Suite)。我之前已经通过以下 MATLAB 脚本成功访问并控制了这个对象:
CST = actxserver('CSTStudio.Application'); % Opens CST
CST.FileNew(); % Creates a new file
MWS = CST.Active3D; % Set focus to the new file
MWS.invoke("AddToHistory","Test",""); % Append to history list
这是我试图用来控制 COM 对象的 C# 代码
cst_design_environmentLib.IApplication CST = new cst_design_environmentLib.Application(); // Opens CST
CST.FileNew(); // Creates a new file
dynamic MWS = CST.Active3D(); // Set focus to the new file
MWS.AddToHistory("Test", ""); // Doesn't work, produces an error
一切正常,直到我尝试运行该方法AddToHistory,该方法失败并出现以下错误:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.__ComObject' does not contain a definition for 'AddToHistory'
我知道该方法AddToHistory存在是因为 1)我通过 MATLAB 访问了它,并且 2)它在文档中。那么为什么它不能通过 C# 运行呢?由于MWS对象是动态类型的,我知道在编译时找不到该方法,但在运行时仍然应该找到它,对吧?我已经调试并检查了对象的类型,并且CST两者MWS都是System.__ComObject. 当我检查这些对象的动态视图时,它显示“无法发现有关此对象的更多信息”,因此它似乎没有找到任何方法。
此外,类型CST是类型库的一部分,cst_design_environmentLib因此FileNew()智能感知预测的方法,但我不相信MWS类型库的类型。
对此的任何帮助将不胜感激。