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?
UPDATE:
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: O开发者_运维技巧bject 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
...
I know this may sound bizarre but I had a similar issue and solved it by wrapping collection with brackets
For Each x in (obj).GetMyObj
Don't know why this would make any difference but it worked for me...
If you get an Object required
error try this:
For Each key in obj.GetMyObj
Set x = obj.GetMyObj(key)
Response.Write x.YOURPROPERTY
Next
Reverse-engineering may solve the problem. Try to pass an ASP classic collection to ASP.NET and then see what the type of object is. Then you can pass that type from ASP.NET to classic ASP instead of a trial-error approach. If there's no relevant type in ASP.NET then you should do more reverse-engineering and explore the binary data for finding the pattern and writing your own casting method.
精彩评论