本文介绍了加载程序集后执行的C#方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些C#类库,我想使用Ninject为我的类提供依赖注入。类库libary可以声明一些代码(方法),将在每个fime执行类库libary加载。我需要这个定义Ninject的绑定。

解决方案

我在过去9个月里使用了Ninject。听起来像你需要做的是加载你的库中的模块到Ninject内核为了注册绑定。



我不确定您使用的是Ninject 1.x还是2.0 beta版。两个版本执行的事情略有不同,但在概念上,它们是相同的。我会坚持使用1.x版本的讨论。我不知道的另一条信息是,如果你的主程序是实例化Ninject内核,你的库只是添加绑定到该内核,或者如果你的库本身包含内核和绑定。我假设您需要在库中将绑定添加到主程序集中的现有Ninject内核。最后,我将假设您动态加载此库,并且它不静态链接到主程序。



首先要做的是在库中定义一个ninject模块,在其中注册所有的绑定 - 你可能已经做过了,但值得一提。例如:

  public class MyLibraryModule:StandardModule {
public override void Load(){
Bind< IMyService>()
.To< ServiceImpl>();
// ... more bindings ...
}
}

现在您的绑定包含在Ninject模块中,您可以在加载程序集时轻松注册它们。这个想法是,一旦加载程序集,您可以扫描从StandardModule派生的所有类型。一旦你有这些类型,你可以加载它们到内核。

  //某处,你定义内核... 
var kernel = new StandardKernel();

// ...然后在其他地方,加载你的库并加载它的模块...

var myLib = Assembly.Load(MyLibrary);
var stdModuleTypes = myLib
.GetExportedTypes()
.Where(t => typeof(StandardModule).IsAssignableFrom(t));


foreach(stdModuleTypes中的类型类型){
kernel.Load((StandardModule)Activator.CreateInstance(type));需要注意的是,您可以将上述代码进一步概括为加载多个库(例如:并注册多个类型。此外,如上所述,Ninject 2具有内置的这种能力 - 它实际上具有扫描目录,加载程序集和注册模块的能力。非常酷。



如果您的场景与我概述的略有不同,可能会采用类似的原则。


I write some C# class libary and I want to use Ninject to provide dependency injection for my classes. Is it possible for class libary to declare some code (method) that would be executed each fime the class libary is loaded. I need this to define bindings for Ninject.

解决方案

I have used Ninject quite a bit over the last 9 months. Sounds like what you need to do is "load" your modules that exist in your libray into the Ninject kernel in order to register the bindings.

I am not sure if you're using Ninject 1.x or the 2.0 beta. The two versions perform things slightly differently, though conceptually, they are the same. I'll stick with version 1.x for this discussion. The other piece of information I don't know is if your main program is instantiating the Ninject kernel and your library is simply adding bindings to that kernel, or if your library itself contains the kernel and bindings. I am assuming that you need to add bindings in your library to an existing Ninject kernel in the main assembly. Finally, I'll make the assumption that you are dynamically loading this library and that it's not statically linked to the main program.

The first thing to do is define a ninject module in your library in which you register all your bindings -- you may have already done this, but it's worth mentioning. For example:

public class MyLibraryModule : StandardModule {
  public override void Load() {
    Bind<IMyService>()
      .To<ServiceImpl>();
    // ... more bindings ...
  }
}

Now that your bindings are contained within a Ninject module, you can easily register them when loading your assembly. The idea is that once you load your assembly, you can scan it for all types that are derived from StandardModule. Once you have these types, you can load them into the kernel.

// Somewhere, you define the kernel...
var kernel = new StandardKernel();

// ... then elsewhere, load your library and load the modules in it ...

var myLib = Assembly.Load("MyLibrary");
var stdModuleTypes = myLib
                       .GetExportedTypes()
                       .Where(t => typeof(StandardModule).IsAssignableFrom(t));


foreach (Type type in stdModuleTypes) {
  kernel.Load((StandardModule)Activator.CreateInstance(type));
}

One thing to note, you can generalize the above code further to load multiple libraries and register multiple types. Also, as I mentioned above, Ninject 2 has this sort of capability built-in -- it actually has the ability to scan directories, load assemblies and register modules. Very cool.

If your scenario is slightly different than what I've outlined, similar principles can likely be adapted.

这篇关于加载程序集后执行的C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 06:04