What are alternatives to generic collections for COM Interop?(COM 互操作的通用集合的替代方案是什么?)
问题描述
我正在尝试从 .NET 程序集中返回一组部门,以供 ASP 通过 COM 互操作使用.使用 .NET 我只会返回一个通用集合,例如List<Department>
,但泛型似乎不适用于 COM 互操作.那么,我有哪些选择?
I am attempting to return a collection of departments from a .NET assembly to be consumed by ASP via COM Interop. Using .NET I would just return a generic collection, e.g. List<Department>
, but it seems that generics don't work well with COM Interop. So, what are my options?
我想遍历列表并能够按索引访问项目.我应该从 List
继承,实现 IList
、IList
或其他接口,还是有更好的方法?理想情况下,我宁愿不必为我需要的每种类型的列表实现自定义集合.此外,List[index]
甚至可以与 COM Interop 一起使用吗?
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? 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?
谢谢,迈克
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
}
}
示例 ASP 代码:
<%
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 %>
相关问题:
- 在服务组件上使用通用列表
- .NET 中的非泛型集合是否已过时?
推荐答案
经过更多的研究和反复试验,我想我找到了使用 System.Collections.ArrayList
的解决方案.但是,这不适用于按索引获取值.为此,我创建了一个继承自 ArrayList
的新类 ComArrayList
,并添加了新方法 GetByIndex
和 SetByIndex
.
After some more research and trial-and-error, I think I found a solution by using 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
.
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;
}
}
更新了 .NET 组件 MyLibrary.GetDepartments:
public ComArrayList GetDepartments() {
// return a list of Departments from the database
}
更新的 ASP:
<h1>The third department</h1>
<%= departments.GetByIndex(2).Name %>
这篇关于COM 互操作的通用集合的替代方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:COM 互操作的通用集合的替代方案是什么?


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01