问题描述
我想我可以编译一个 C# DLL,然后将它公开为一个 COM 对象,以便它可以从 VBscript 中创建对象.我只是不确定执行此操作所涉及的步骤...
I imagine I can compile a C# DLL and then expose it as a COM object so that it can be CreateObject'd from VBscript. I'm just not sure the steps involved in doing this...
推荐答案
这样做非常简单.但也有很多地方没那么简单.这在很大程度上取决于您的班级需要能够做什么,以及您打算如何部署它.
It can be very simple to do this. But there are a lot of places where it's not so simple. It depends a lot on what your class needs to be able to do, and how you intend to deploy it.
需要考虑的一些问题:
- 你的类必须有一个无参数的构造函数.
- 它不能暴露静态方法.
- 在全局程序集缓存中部署您的 COM DLL 好吗?如果没有,您将不得不给它一个强名称并使用
regasm/codebase
注册它. - 您是否关心标识类及其接口的 GUID 是什么?如果没有,您可以让
regasm
分配它们,但是每次(和每个地方)注册课程时它们都会有所不同.如果您需要 GUID 在安装过程中保持不变,则需要使用Guid
属性标记成员. - 您打算在 .NET 和 COM 之间使用数据类型的默认编组吗?如果没有,您将需要使用
MarshalAs
属性标记属性和方法. - 你的类公开什么样的 COM 接口对你来说很重要吗?如果是这样,您将需要处理
InterfaceType
属性. - 您的班级是否需要提出或回应事件?如果是这样,就会对您设计类界面的方式产生影响.
- Your class has to have a parameterless constructor.
- It can't expose static methods.
- Is deploying your COM DLL in the global assembly cache OK? If not, you're going to have to give it a strong name and register it using
regasm /codebase
. - Do you care what GUIDs identify the class and its interfaces? If not, you can let
regasm
assign them, but they'll be different every time (and every place) the class is registered. If you need the GUIDs to remain invariant across installations, you'll need to mark members with theGuid
attribute. - Are you going to use the default marshaling of data types between .NET and COM? If not, you're going to need to mark properties and methods with the
MarshalAs
attribute. - Does it matter to you what kind of COM interface your class exposes? If so, you're going to need to deal with the
InterfaceType
attribute. - Does your class need to raise or respond to events? If so, there are implications for how you design your class interface.
有一篇关于 COM 互操作和 .Net 的非常好的(如果过时的话)文章这里一个>.(文章谈到的很多事情,比如生成类型库,现在都是自动为您处理的.)和 Microsoft 的文档 是最新的,但不是很详细.
There's a very good (if dated) article about COM interop and .Net here. (A lot of things that article talks about, like generating type libraries, is handled for you automatically now.) And Microsoft's documentation is up to date, but not quite so detailed.
这篇关于如何从 vbScript 调用 .NET 代码 (C#/vb.net)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!