DI with WinRT ComObject support

本文关键字:support ComObject WinRT with DI | 更新日期: 2023-09-27 18:26:47

我刚刚尝试将Unity DI用于基本DI。这是我的设置:

C#组件项目

public interface ILanguageBindings { ... }

C++/CX组件项目

private ref class LanguageBindings : ILanguageBindings { ... }

public ref class LanguageImplementation {
public:
    ILanguageBindings GetLanguageBindings();
}

C#可移植

public class Bootstrap {
    private UnityContainer container;
    public void Initialize(ILanguageBindings language) {
        this.container.RegisterInstance<ILanguageBindings>(language);
    }
}

C#应用程序

var languageImpl = new LanguageImplementation();
var languageBindings = languageImpl.GetLanguageBindings();
var bootstrap = new Bootstrap();
bootstrap.Initialize(languageBindings);

我从UnityContainer收到以下错误消息:

An exception of type 'System.ArgumentException' occurred in 
Microsoft.Practices.Unity.DLL but was not handled in user code
Additional information: The type System.__ComObject cannot be 
assigned to variables of type SparkiyEngine.Bindings.Language.ILanguageBindings.

我使用的是3.5.1405-prerelease版本(适用于Windows和Windows Phone)。有没有一种方法可以让它与Unity容器一起使用,因为我在项目的其余部分中都在使用它。如果没有,支持ComObjects的替代方案是什么?

完整的代码在GitHub上,我刚刚推送了有问题的代码。

DI with WinRT ComObject support

通过更改解决

private ref class LanguageBindings : ILanguageBindings { ... }

public ref class LanguageBindings sealed : ILanguageBindings { ... }