How do I call .NET code (C#/vb.net) from vbScript?(如何从 vbScript 调用 .NET 代码 (C#/vb.net)?)
问题描述
我想我可以编译一个 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
regasmassign 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 theGuidattribute. - 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
MarshalAsattribute. - Does it matter to you what kind of COM interface your class exposes? If so, you're going to need to deal with the
InterfaceTypeattribute. - 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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 vbScript 调用 .NET 代码 (C#/vb.net)?
基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 将数据集转换为列表 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
