首页 > 解决方案 > COM 互操作,创建 .net com 并在 .net 中使用

问题描述

我有一个公开 com 接口的程序集:

  [ComVisible(true)]
  [Guid("C92A5F13-088F-4776-A967-42A60EFAFC29")]  
  public interface ITestInterface
  {
    byte[] AddItem(string label, byte[] array);
  }

和实施:

  [ComVisible(true)]
  [Guid("C35C9235-3377-44A7-AC9E-7FCC24EF71D0")]    
  public class Test : ITestInterface
  {
    public byte[] AddItem(string label, byte[] array)
    {
      return null;//irrelevant
    }
  }

该 com 在程序集的清单中发布。

现在在客户端(.net),我有一个界面:

  public interface ITestInterface
  {
    byte[] AddItem(string label, byte[] array);
  }

并尝试激活它:

Type adapter = Type.GetTypeFromProgID("Test.Test");                 
object adapterObj = Activator.CreateInstance(adapter);

ITestInterface myClassAdapter = (ITestInterface)adapterObj;

在最后一行我得到异常:

System.InvalidCastException: 'Unable to cast object of type 'Test.Test' to type 'Test.ITest'.'

我的“客户端”界面有什么问题。我应该改变什么?

标签: c#.netinterop

解决方案


推荐阅读