26 November 2009

Alternatives to generic collections for COM Interop

After some more research and trial-and-error, I think I found a solution by usingSystem.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 methodsGetByIndex and SetByIndex.

COM Interop compatible collection:

 

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 component MyLibrary.GetDepartments:

public class Department {
    public string Code { get; private set; }
    public string Name { get; private set; }
    // ...
}

public ComArrayList GetDepartments() {
    // return a List(of Department) from the database
}

ASP:

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