0

I have a number of functions that look basically like…

 Public Function Keys() As Dictionary(Of Integer, myData).KeyCollection

I'm trying to expose this class to COM so I can use it in Excel, but VS complains that…

 Warning: Type library exporter encountered a generic type instance in a signature. Generic code may not be exported to COM.

I think this means I have to copy this to a type that can be exported to COM, I suspect a SAFEARRAY of strings?

Anyone have an example code of this?

1 Answer 1

1

You cannot expose generic types to COM, it has no support for it whatsoever. The reason that Dictionary doesn't have the [ComVisible(true)] attribute. Somewhat intuitively obvious, languages like C++, scripting languages like VBScript or Javascript, VB6 and VBA, etc, don't know beans about .NET generics. Only a .NET program can ever use it, you don't need COM interop for that.

If you want to expose a dictionary then you'll need to create your own or simply fall back to the olden Hashtable. Which is [ComVisible], your type library will automatically include mscorlib.tlb

Sign up to request clarification or add additional context in comments.

4 Comments

It's not the dictionary, it's an array of strings representing the dictionary keys. Any sort of "array of strings" or "list of strings" or anything like that should work.
Well, of course you can expose an array of strings. ArrayList is fine too. As long as it is not KeyCollection, that's generic. Clearly I don't really understand the problem.
Ok, so is there an easy way to turn a KeyCollection into an ArrayList? Looping is fine, but I'd prefer something already in the API.
Well, sure, it's but a sliver of code: arrlist.AddRange(dict.Keys). That does not make it cheap. Design your ComVisible class to be as compatible as possible with the client code. Copying dictionaries should always be avoided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.