views:

45

answers:

1

For the sake of this question, here is my generic class.

[ComVisible(true)]
public class HtmlTable<T> where T : class
{
    List<T> listToConvert;

    public HtmlTable(List<T> listToConvert)
    {
        this.listToConvert = listToConvert;
    }
}

Essentially, this class is responsibly for converting a List of class T to an HTML table (I've omitted the generating sections).The error I get is

Generic classes may not be exposes to COM.

I have read several posts regarding the issue, however I don't really understand them. What needs to be changed/added in order to be able to use this class?

Thank you.

+4  A: 

COM has no concept of Generics, therefore Generic classes can't be exposed to COM.

What you can do is create a Generic super type, and then create Non-Generic specific implementations of that Generic super type to expose to COM.

A quick example:

public class HtmlTable<T> where T : class 
{ 
    List<T> listToConvert; 

    public HtmlTable(List<T> listToConvert) 
    { 
        this.listToConvert = listToConvert; 
    } 
} 

[ComVisible(true)]
public class StringHtmlTable : HtmlTable<String>
{
    // implementation goes here
}
Justin Niessner