COM 互操作的通用集合的替代方案是什么?

What are alternatives to generic collections for COM Interop?(COM 互操作的通用集合的替代方案是什么?)
本文介绍了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 继承,实现 IListIList 或其他接口,还是有更好的方法?理想情况下,我宁愿不必为我需要的每种类型的列表实现自定义集合.此外,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,并添加了新方法 GetByIndexSetByIndex.

    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 互操作的通用集合的替代方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)