如何从vbScript调用.NET代码(C#/ vb.net)? [英] How do I call .NET code (C#/vb.net) from vbScript?

查看:506
本文介绍了如何从vbScript调用.NET代码(C#/ vb.net)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可以编译一个C#DLL,然后将它公开为一个COM对象,以便它可以从VBscript的CreateObject。我只是不确定这样做的步骤...

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 此处。 (很多文章谈到的东西,如生成类型库,现在自动处理。)和 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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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