从 C# 访问 COM 对象的最佳方法 [英] Best way to access COM objects from C#

查看:26
本文介绍了从 C# 访问 COM 对象的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划使用作为 COM 对象公开的各种对象.为了使它们更易于使用,我想将它们包装为 C# 对象.最好的方法是什么?

I am planning to use various objects that are exposed as COM objects. To make them easier to use, I'd like to wrap them as C# objects. What is the best approach for this?

推荐答案

如果库已经注册,您可以执行以下步骤让 Visual Studio 为您生成互操作程序集:

If the library is already registered, you can perform the following steps to have Visual Studio generate an interop assembly for you:

  • 打开您的 Visual Studio 项目.
  • 右键单击参考"(位于解决方案资源管理器中的项目下方)并选择添加参考".
  • 选择 COM 选项卡.(如果您没有看到这一点,则说明您的项目类型不支持 COM.)
  • 选择您希望与之交互的组件.
  • 选择确定".

这将是一个类或一组 C# 类,用普通 C# 类包装所有 COM 接口内容.然后,您只需像使用任何其他 C# 库一样使用它.如果引用的导入运行良好,您可以像任何其他引用一样探索它,并且方法/结构/类/常量应该显示在该命名空间和智能感知中.

This will be a class or set of C# classes that wrap all of the COM interface stuff with a normal C# class. Then you just use it like any other C# library. If the import of the reference worked well, you can explore it like any other reference and the methods/structs/classes/constants should show up in that namespace and intellisense.

这至少可以帮助您入门.如果将其部署在企业环境中或您可以控制的环境中,这可能就是您所需要的.

This will get you started, at least. If this is deployed in a corporate environment or one you can control, this may be all you need.

这篇关于从 C# 访问 COM 对象的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆