Howto author a scriptable collection in C#
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?
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?
精彩评论