【问题标题】:What are alternatives to generic collections for COM Interop?COM 互操作的通用集合的替代方案是什么?
【发布时间】:2008-11-06 17:43:38
【问题描述】:

我试图从 .NET 程序集中返回一组部门,以供 ASP 通过 COM 互操作使用。使用 .NET 我只会返回一个通用集合,例如List<Department>,但泛型似乎不适用于 COM 互操作。那么,我有哪些选择?

我想遍历列表并能够按索引访问项目。我应该继承List<Department>,实现IListIList<Department> 或其他接口,还是有更好的方法?理想情况下,我宁愿不必为我需要的每种类型的列表实现自定义集合。另外,List[index] 甚至可以与 COM 互操作一起使用吗?

谢谢, 迈克

.NET 组件示例 (C#):

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 generics collections asp-classic com-interop


    【解决方案1】:

    经过更多的研究和反复试验,我想我找到了使用System.Collections.ArrayList 的解决方案。但是,这不适用于按索引获取值。为此,我创建了一个新类 ComArrayList,它继承自 ArrayList,并添加了新方法 GetByIndexSetByIndex

    COM 互操作兼容集合:

    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 %>
    

    【讨论】:

      【解决方案2】:

      由于您只使用 ASP 中的数据,我建议返回 Department[]。这应该直接映射到 COM 中的 SAFEARRAY。它也支持枚举和索引访问。

      public Department[] GetDepartments() {
          var departments = new List<Department>();
          // populate list from database
          return departments.ToArray();
      }
      

      【讨论】:

        【解决方案3】:

        在你的 ASP 代码中,你不能这样做吗?

        <h1>The third department</h1>
        <%= departments.Item(2).Name %>
        

        我知道在 VB .NET 中,C# 索引器是通过“Item”属性支持的,所以同样的想法也可以在 ASP 中使用。

        【讨论】:

          【解决方案4】:

          我一直在尝试在 vb.net 中解决同样的问题(不熟悉 C#)。

          由于 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
          

          然后在 COM 看到的公共接口中指定

          ReadOnly Property MyList As IList
          

          这似乎在经典的 ASP 客户端中可以正常工作,该客户端实例化对象,调用生成器函数,然后像数组一样读取 MyList 属性。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-12-25
            • 1970-01-01
            • 2021-11-13
            • 2018-12-01
            • 1970-01-01
            • 1970-01-01
            • 2015-03-21
            相关资源
            最近更新 更多