DI与WinRT ComObject支持

时间:2014-10-17 04:21:02

标签: c# dependency-injection windows-store-apps unity-container c++-cx

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

C#组件项目

public interface ILanguageBindings { ... }

C ++ / CX组件项目

private ref class LanguageBindings : ILanguageBindings { ... }


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

C#Portable

public class Bootstrap {
    private UnityContainer container;

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

C#App

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上,我刚刚推出了有问题的代码。

1 个答案:

答案 0 :(得分:0)

通过更改解决

private ref class LanguageBindings : ILanguageBindings { ... }

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