如何正确地将 VB-Script 数组编组到用 C# 编写的 COM 组件或从中编组
我正在 C# (.Net 4.0) 中构建一个 COM 对象,以便在经典的 asp 站点中使用。现在我想知道在组件和 asp 站点之间来回编组 VB-Script 数组(单维和多维)的正确方法是什么?代码示例将受到高度赞赏。
I'm building a COM object in C# (.Net 4.0) to be used in an classic asp site. Now I'd like to know what's the proper way to marshal VB-Script arrays (single and multidimensional) back and forth between the component and the asp site? A code sample would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
VBScript 只喜欢处理包含 VARIANTS 的 SAFEARRAY。它喜欢将这些传递到 COM 方法或属性的 VARIANTS 中。因此,您需要构造一个包含 VARIANT 类型的 SAFEARRAY 的 VARIANT 属性。以下 C# 代码执行此操作。首先仅使用一个普通的对象数组,然后还表明我们可以将任何其他托管类型的数组转换为对象数组,以便编组代码将其转换为我们的 VARIANT 的 SAFEARRAY。
可以使用以下 vbscript 进行测试:
您可以看到此处报告的类型为 Variant() - 即:变体数组。
VBScript only likes to handle SAFEARRAY's that contain VARIANTS. And it likes to have these passed arround in VARIANTS on the COM methods or properties. So you need to construct a VARIANT property that contains a SAFEARRAY of VARIANT type. The following C# code does this. First using just a plain array of objects and then also showing we can cast an array of any other managed type into an array of objects such that the marshalling code will convert this into a SAFEARRAY of VARIANTs for us.
This can be tested using the following vbscript:
You can see the type reported here as Variant() - ie: an array of variants.
与其说是答案,不如说是一些附加信息:
This is how to Consumer patthoyts' answer in Classic ASP using VBScript:
I无法访问单个数组元素(例如o.Names(2)),这表明它不是数组但表现得更像是一个集合。
JScript 版本:
Not so much an answer but some additional information:
This is how to consume patthoyts' answer in Classic ASP using VBScript:
I cannot access the individual array elements (eg. o.Names(2)) which indicates that it isn't an array but acting more like a collection.
JScript version:
有点晚了,但以防将来有人需要这个:
我设法将
Hashtables
的ArrayList
传递给 Classic ASP。看来命名空间System.Collections
的类型可以传递,而System.Collections.Generic
则不可以。.cs-File:
.asp-File:
输出:
如果您必须将大量数据从 C# 传递到经典 ASP(也应该在 VB 脚本中工作,但未经测试),这非常方便,因为您可以使用以下命令循环访问对象任何属性。也没有测试相反的方式,因为我只需要将数据从 C# 传递到经典 ASP。
A bit late, but in case someone needs this in the future:
I managed to pass an
ArrayList
ofHashtables
to Classic ASP. It seems that types of the namespaceSystem.Collections
can be passed,System.Collections.Generic
can not..cs-File:
.asp-File:
Output:
This is pretty convenient if you have to pass a lot of data from C# to Classic ASP (Should work in VB Script too, but not tested), as you can loop through objects with any attributes. Also didn't test the other way around, because I only needed to pass data from C# to Classic ASP.