Unity - 如何为 COM Interop DLL 注册类型

Unity - How to RegisterType for a COM Interop DLL

我有一个旧的 COM Interop dll,我必须将它包含在我的 .Net 4.5.1 应用程序中。该应用程序使用 Unity 进行 IoC 和 DI。

Interop dll 具有以下简化的内容:

public interface ILegacyInterop
{
    void DoStuff()
}

public interface LegacyInterop : ILegacyInterop
{
}

public class LegacyInteropClass : ILegacyInterop, LegacyInterop 
{
    public LegacyInteropClass();

    public virtual void DoStuff();
}

我试过如下连接接口

container.RegisterType<ILegacyInterop, LegacyInterop>();

container.RegisterType<ILegacyInterop, LegacyInteropClass>();

container.RegisterType<LegacyInterop, LegacyInteropClass>();

None 其中工作。

第一个选项在 运行 上给出了以下错误:

InvalidOperationException - The current type, LegacyInterop, is an interface and cannot be constructed. Are you missing a type mapping?

第二个和第三个选项在构建时给出以下错误:

Interop type 'LegacyInteropClass' cannot be embedded. Use the applicable interface instead.

我想做的事情真的可行吗?如果是,怎么做?

我可以通过实例化 LegacyInterop

让互操作工作

我在 Interop type cannot be embedded

的帮助下弄明白了

有问题的 Interop 参考需要 Embed Interop Assembly 设置为 False

然后 container.RegisterType<ILegacyInterop, LegacyInteropClass>(); 有效