COM Interop使用通用类型

时间:2009-08-14 13:03:47

标签: c# com interop

您似乎无法直接在COM上使用泛型类型:请参阅MSDN上的Interoperating Using Generic Types

有人能举例说明如何实现这一目标吗?

1 个答案:

答案 0 :(得分:4)

我认为您的想法是不能直接将通用类型标记为ComVisible,但您可以使用该类型实现ComVisible的非通用类型。

因此,给定通用Baker<Recipe>,您需要引入类似的内容:

[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().
   }
}

我认为这是文章所讨论的“间接”。我不太清楚VB.NET的Controls集合与此有什么关系,但是......