Consume Result of .NET Function Returning LIST(Of Type) in Classic ASP

混江龙づ霸主 提交于 2020-01-06 11:34:18

问题


Imagine the following VB.NET class:

Public Class P
    Public Function GetDumbList() As List(Of DumbPeople)
        Dim retList As New List(Of DumbPeople)
        retList.Add Person1
        retList.Add Person2
        Return retList
    End Function
End Class

Now we have the following Classic ASP Page:

<%
Dim P
Set P = Server.CreateObject("Project.Assembly.Namespace.P")

retList = P.GetDumbList()
...
...
...
%>

How do you use retList? I have tried looping through using the 2 following methods:

1. For Each Person in retList  

Throws error "Object not a collection"

2. For i = 0 To Ubound(retList)

Throws error "Type mismatch: -UBound-

Thanks in advance for your help. Jake

UPDATE

Based on help from Chris Haas we were able to solve the issue. It does require a second ASPClassic helper function to convert the List(of T) to an Object array; however, COM objects cannot expose generic methods. Because of this, the input must be a specific type of List to be converted to the Object array. Solution Below

Public Function DumbPeopleListToObjectArray(ByVal DPList As IList(Of DumbPeople)) As Object()
    Return Array.ConvertAll(Of DumbPeople, Object)(DPList.ToArray(), New Converter(Of DumbPeople, Object)(Function(j) DirectCast(j, Object)))
End Function

Thank you to Chris Haas for putting me on the right direction. Hope this helps someone else.


回答1:


I don't think you're going to be able to use generics with ASP Classic, I think you're going to need to convert it to an array. You could write a utility method for ASP Classic usage that takes a IList(Of T) and returns an Object(). Its an extra step for ASP Classic people to jump through but not the end of the world.

EDIT

Something like this might do the trick:

Public Shared Function ToASPClassicArray(Of T)(ByVal myList As IList(Of T)) As T()
    Return myList.ToArray()
End Function

EDIT 2

Here's another version that returns just an object, pretty ugly but should still work.

Public Shared Function ToASPClassicArray(Of T)(ByVal myList As IList(Of T)) As Object()
    Return Array.ConvertAll(Of T, Object)(myList.ToArray(), New Converter(Of T, Object)(Function(j) DirectCast(j, Object)))
End Function


来源:https://stackoverflow.com/questions/6684495/consume-result-of-net-function-returning-listof-type-in-classic-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!