解决跨项目的Interop依赖关系

时间:2012-11-27 11:42:43

标签: interop ninject excel-interop

另一个电报式问题。首先,我的NinjectModule:

using Ninject.Modules;
using Microsoft.Office.Interop.Word;

namespace NinjectSample.Util.Injection
{
    public class SampleModule : NinjectModule
    {
        public override void Load()
        {
            Bind<_Application>().ToMethod(ctx => GetApp());
        }

        public _Application GetApp()
        {
            return new Application();
        }
    }
}

第一件事(有效!):

        IKernel kernel = new StandardKernel(new SampleModule());
        var foo = kernel.Get<_Application>();

将此更改为

        IKernel kernel = new StandardKernel(new SampleModule());
        var foo = kernel.Get<BusinessClass>();

BusinessClass在另一个程序集中定义,代码:

namespace BusinessClassLibrary
{
    public class BusinessClass
    {
        private _Application _app;

        [Inject]
        public BusinessClass(_Application application)
        {
            _app = application;
        }
    }
}

这将导致:

Error activating _Application
No matching bindings are available, and the type is not self-bindable.
Activation path:
  2) Injection of dependency _Application into parameter application of constructor of type BusinessClass
  1) Request for BusinessClass

Suggestions:
  1) Ensure that you have defined a binding for _Application.
  2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
  3) Ensure you have not accidentally created more than one kernel.
  4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
  5) If you are using automatic module loading, ensure the search path and filters are correct.

我不太喜欢互操作,但我对.Net的基本理解让我偶然发现了以下内容:

  • Application是一个界面,就像它的基础_Application一样。然而,可以调用它的构造函数。为什么呢?

  • 我告诉Ninject使用一个工厂方法,它似乎在依赖项位于定义内核的程序集中时起作用。为什么它在位于的时候通过自绑定来尝试解析依赖项另一个集会?

1 个答案:

答案 0 :(得分:1)

  

应用程序是一个接口,就像它的基础_Application一样。   然而,可以调用它的构造函数。为什么呢?

这是一个编译技巧;有关正在发生的事情的信息,请参阅Creating instance of interface in C#。当你在代码中遇到它时,它看起来很奇怪。不幸的是,目前无法帮助您解决问题的实际Ninject部分。

相关问题