tags:

views:

284

answers:

1

It appears you can not use generic types directly with COM: see Interoperating Using Generic Types on MSDN.

Can anyone give an example of how this could be achieved?

+3  A: 

I think the idea is that you can't mark a generic type as ComVisible directly, but you can have that type implement non-generic interfaces that are ComVisible.

So, given a generic Baker<Recipe>, you would need to introduce something like:

[ComVisible(true)]
public interface IBake
{
   Pastry Bake();
}

public class Baker<Recipe> : IBake
{
   public Baker(Recipe ingredients) {...}

   public Pastry Bake()
   {
      ...
   }
}

[ComVisible(true)]
public class Bakery
{
   public IBake GetBaker(string recipe)
   {
      // somehow get recipe type from string
      // and create and return Baker<Recipe>
      // Client can now call IBake.Bake().
   }
}

I suppose this is the "indirectly" that the article is talking about. I don't quite see what VB.NET's Controls collection has to do with this, however...

Kim Gräsman
I think this is actually a bug (I don't know if it is with .net framework or COM Interop when using generic types.To explain (using vb.net) :If I allow VB.NET to work out what type of object the 'generic' designer object it, it correctly works out it is a userform (I know this by returning the type :Microsoft.VisualBasic.Information.TypeName). If I expicitly set the generic object as a Userform, I get an error (in any .NET language) : {"The server threw an exception. (Exception from HRESULT: 0x80010105(RPC_E_SERVERFAULT))"}
jedd
You lost me there, I'm afraid...
Kim Gräsman