如何使用 C# 创作可编写脚本的集合
我需要编写一个通过 COM Interop 作为 COM 对象可见的 .NET 程序集,以便它可以向 VBScript 或 JScript 程序提供集合。
当我使用简单的集合(例如 ArrayList)时,它通过 COM 互操作流动,并且可以使用如下程序在 VBScript 中进行迭代:
Dim s, foo
Set foo = CreateObject("Whatever.Collection")
For Each s In foo
WScript.Echo s
Next
现在,如果我使用基于通用集合类的某种东西来代替 ArrayList,例如 List
List< string>
或 System.Collections.Specialized.StringCollection
,这似乎在 VBScript 中不起作用。我在 For Each 循环中遇到错误,例如
Microsoft VBScript 运行时错误:需要对象:
所以,似乎有一些我在这里缺少的魔法酱。怎样才能使 .NET 集合通过 COM Interop 流动并在 VBScript 和 JScript 中正常工作?
I need to author a .NET assembly that is visible as a COM object through COM Interop, such that it can supply a collection to a VBScript or JScript program.
When I use simple collections such as ArrayList, that flows through COM interop and can be iterated over in VBScript using a program like this:
Dim s, foo
Set foo = CreateObject("Whatever.Collection")
For Each s In foo
WScript.Echo s
Next
Now, if instead of ArrayList I use somethign based on a generic collection class, such as List<string>
, or System.Collections.Specialized.StringCollection
, that doesn't seem to work in the VBScript. I get errors in the For Each loop, like
Microsoft VBScript runtime error: Object required:
So, it seems like there is some magic sauce that I'm missing here somewhere. What does it take to make a .NET collection flow through COM Interop and work correctly in VBScript and JScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我开始寻找是否有解决方案,因为我认为这是不可能的。 Rick Strahl 似乎有一个解决方案。
编辑:这是SO COM Interop 的通用集合的替代方案是什么?
I started looking to see if there is a solution to this since I didn't think it was possible. It seems like Rick Strahl has a solution.
Edit: Here is another solution on SO What are alternatives to generic collections for COM Interop?