[英]What are alternatives to generic collections for COM Interop?
I am attempting to return a collection of departments from a .NET assembly to be consumed by ASP via COM Interop.我正在尝试从 .NET 程序集中返回一组部门,以通过 COM Interop 由 ASP 使用。 Using .NET I would just return a generic collection, eg List<Department>
, but it seems that generics don't work well with COM Interop.使用 .NET 我只会返回一个泛型集合,例如List<Department>
,但似乎泛型不能很好地与 COM Interop 一起使用。 So, what are my options?那么,我有哪些选择?
I would like to both iterate over the list and be able to access an item by index.我想遍历列表并能够按索引访问项目。 Should I inherit from List<Department>
, implement an IList
, IList<Department>
or another interface, or is there a better way?我应该从List<Department>
继承,实现IList
、 IList<Department>
或其他接口,还是有更好的方法? Ideally I would prefer not to have to implement a custom collection for every type of list I need.理想情况下,我不想为我需要的每种类型的列表实现自定义集合。 Also, will List[index]
even work with COM Interop?此外, List[index]
甚至可以与 COM Interop 一起使用吗?
Thanks, Mike谢谢,迈克
public class Department {
public string Code { get; private set; }
public string Name { get; private set; }
// ...
}
public class MyLibrary {
public List<Department> GetDepartments() {
// return a list of Departments from the database
}
}
<%
Function PrintDepartments(departments)
Dim department
For Each department In departments
Response.Write(department.Code & ": " & department.Name & "<br />")
Next
End Function
Dim myLibrary, departments
Set myLibrary = Server.CreateObject("MyAssembly.MyLibrary")
Set departments = myLibrary.GetDepartments()
%>
<h1>Departments</h1>
<% Call PrintDepartments(departments) %>
<h1>The third department</h1>
<%= departments(2).Name %>
After some more research and trial-and-error, I think I found a solution by using System.Collections.ArrayList
.经过更多的研究和反复试验,我想我找到了使用System.Collections.ArrayList
的解决方案。 However, this does not work with getting a value by index.但是,这不适用于按索引获取值。 To do so, I created a new class ComArrayList
that inherits from ArrayList
and adds new methods GetByIndex
and SetByIndex
.为此,我创建了一个从ArrayList
继承的新类ComArrayList
并添加了新方法GetByIndex
和SetByIndex
。
public class ComArrayList : System.Collections.ArrayList {
public virtual object GetByIndex(int index) {
return base[index];
}
public virtual void SetByIndex(int index, object value) {
base[index] = value;
}
}
public ComArrayList GetDepartments() {
// return a list of Departments from the database
}
<h1>The third department</h1>
<%= departments.GetByIndex(2).Name %>
Since you are only consuming the data in ASP, I would suggest returning Department[]
.由于您只使用 ASP 中的数据,我建议返回Department[]
。 This should map directly to a SAFEARRAY in COM.这应该直接映射到 COM 中的 SAFEARRAY。 It supports enumeration and indexed access too.它也支持枚举和索引访问。
public Department[] GetDepartments() {
var departments = new List<Department>();
// populate list from database
return departments.ToArray();
}
In your ASP code, could you not do this?在您的 ASP 代码中,您可以不这样做吗?
<h1>The third department</h1>
<%= departments.Item(2).Name %>
I know that in VB .NET, C# indexers are supported via the "Item" property, so the same idea may work in ASP.我知道在 VB .NET 中,通过“Item”属性支持 C# 索引器,因此同样的想法可能适用于 ASP。
I've been trying to address this same problem in vb.net (not fluent in C#).我一直在尝试在 vb.net 中解决同样的问题(不精通 C#)。
Since List(Of T) supports the IList interface, for the COM interface for my objects I have tried specifying由于 List(Of T) 支持 IList 接口,对于我的对象的 COM 接口,我尝试指定
Public Class MyClass
...
Private _MyList As List(of MyObject)
Public ReadOnly Property MyList As IList Implements IMyClass.MyList
Get
Return _MyList
End Get
End Property
and then specifying in the Public Interface that COM sees然后在 COM 看到的公共接口中指定
ReadOnly Property MyList As IList
This seems to work fine from a classic ASP client that instantiates the object, calls a generator function and then reads MyList property like an array.这似乎在经典的 ASP 客户端中工作正常,该客户端实例化对象,调用生成器函数,然后像数组一样读取 MyList 属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.