如何从VBScript中调用.NET code(C#/ vb.net)?NET、VBScript、code、net

2023-09-04 01:38:02 作者:疯子没有疯

我想我可以编译C#DLL,然后公开为COM对象,以便它可以从VBScript来CreateObject'd。我只是不知道参与这样的步骤...

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 the Guid 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 。 (很多事情那篇文章谈到了,比如生成类型库,自动为您现在的处理。)和微软的文档是最新的,但没有这么详细。

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.