I am having trouble exposing a C# collection to Classic ASP.
I've tried to use IEnumerable and Array. but I get the "object not a collection
" error.
my method looks like:
public IEnumerable<MyObj> GetMyObj() { ... }
and on the Classic ASP side:
Dim obj, x
Set obj = Server.CreateObject("Namespace.class")
For Each x in obj.GetMyObj
...
So how can I pass a collection to Classic ASP?
may be this is a progress, the solution I found was to use a new class that inherits IEnumerable
instead of using IEnumerable<MyObj>
:
public class MyEnumerable : IEnumerable
{
private IEnumerable<MyObj> _myObj;
.
.
.
[DispId(-4)]
public IEnumerator GetEnumerator()
{
_myObj.GetEnumerator();
}
}
But now when I try to access a property of MyObj
I get an error: Object required
.
Any idea?
I think you found the answer; Classic ASP with VB does not have generics built into it. So, you try to pass an IEnumerable<MyObj>
to ASP and it comes out as a nightmarish mashup name which classic ASP has no clue how to work with.
The solution is to pass a non-generic IEnumerable. The problem then is that the sequence is treated as containing basic Object instances. You must revert to the pre-2.0 methods of casting objects you get out of a list. In VB this isn't difficult; just explicitly specify the element type and VB will implicitly cast for you while iterating through the For Each:
Dim obj, x
Set obj = Server.CreateObject("Namespace.class")
For Each x As MyObj in obj.GetMyObj //casts each element in turn
...