我只是尝试将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上,我刚刚推出了有问题的代码。
答案 0 :(得分:0)
通过更改解决
private ref class LanguageBindings : ILanguageBindings { ... }
到
public ref class LanguageBindings sealed : ILanguageBindings { ... }