简体   繁体   English

如何从vbScript调用.NET代码(C#/ vb.net)?

[英]How do I call .NET code (C#/vb.net) from 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. 我想我可以编译一个C#DLL,然后将它作为COM对象公开,以便它可以从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. 这很大程度上取决于您的课程需要做什么,以及您打算如何部署它。

Some issues to consider: 需要考虑的一些问题:

  • 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? 是否在全局程序集缓存中部署COM DLL? If not, you're going to have to give it a strong name and register it using regasm /codebase . 如果没有,您将不得不给它一个强名称并使用regasm /codebase注册它。
  • Do you care what GUIDs identify the class and its interfaces? 您是否关心GUID识别类及其接口? If not, you can let regasm assign them, but they'll be different every time (and every place) the class is registered. 如果没有,你可以让regasm分配它们,但是每次(和每个地方)这个类都注册时它们会有所不同。 If you need the GUIDs to remain invariant across installations, you'll need to mark members with the Guid attribute. 如果您需要GUID在安装期间保持不变,则需要使用Guid属性标记成员。
  • Are you going to use the default marshaling of data types between .NET and COM? 您打算在.NET和COM之间使用默认的数据类型封送吗? If not, you're going to need to mark properties and methods with the MarshalAs attribute. 如果没有,您将需要使用MarshalAs属性标记属性和方法。
  • Does it matter to you what kind of COM interface your class exposes? 你的班级暴露什么样的COM接口对你有用吗? If so, you're going to need to deal with the InterfaceType attribute. 如果是这样,您将需要处理InterfaceType属性。
  • Does your class need to raise or respond to events? 您的班级是否需要提出或回应事件? If so, there are implications for how you design your class interface. 如果是这样,那么对您设计类接口的方式有影响。

There's a very good (if dated) article about COM interop and .Net here . 有一个关于COM互操作和.Net一个很好的(如果日)的文章在这里 (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. (文章讨论的很多内容,比如生成类型库,现在都会自动处理。)而且Microsoft的文档是最新的,但不是那么详细。

You should use the regasm utility to register an assembly (just like you do regsvr32 with COM servers). 您应该使用regasm实用程序来注册程序集(就像使用COM服务器执行regsvr32一样)。 Then you can use it from COM. 然后你可以在COM中使用它。 Make sure it's installed in the GAC. 确保它已安装在GAC中。 The stuff should have [ComVisible(true)] to be usable from COM. 这些东西应该可以从COM中使用[ComVisible(true)]

There's a "COM Accessible" option for each project you can select. 您可以选择的每个项目都有一个“COM Accessible”选项。 Then, of course, you have to register the assembly to use it, but that's about it. 然后,当然,你必须注册程序集才能使用它,但这就是它。


I had to go hunting a bit to find it for C# (I'm used to VB.Net), but you can find the option by double-clicking on the Properties folder in the Solution Explorer, choosing the Application tab and then clicking the Assembly Information... button. 我不得不去寻找C#(我习惯VB.Net),但你可以通过双击解决方案资源管理器中的Properties文件夹找到该选项,选择Application选项卡,然后单击Assembly Information...按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM