本文介绍了什么是替代泛型集合为COM互操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从.NET程序集返回部门的集合是由ASP通过COM互操作所消耗。使用.NET我只想返回一个泛型集合,例如: 名单,其中,部> ,但似乎泛型不与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?

我想这两个遍历列表,并可以通过索引访问的项目。我应该继承名单,其中,部&GT; ,实施的IList 的IList&LT;部&GT; 或其它接口,或者是有没有更好的办法?理想情况下我会preFER不具有实现自定义集合每个类型列表中,我需要。此外,将列表[指数] 甚至与COM互操作工作?

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 code:

<%
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非泛型集合过时了吗?
  • Related questions:

    • Using Generic lists on serviced component
    • Are non-generic collections in .NET obsolete?
    • 推荐答案

      在一些更多的研究,试验和错误,我想我找到了一个解决方案,通过使用 System.Collections.ArrayList 。然而,这并不与由索引得到一个值工作。要做到这一点,我创建了一个新的类 ComArrayList ,它继承自的ArrayList ,并增加了新的方法 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互操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:03
查看更多