Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 236043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:18:48+00:00 2026-05-11T20:18:48+00:00

I write some C# class libary and I want to use Ninject to provide

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:18:49+00:00Added an answer on May 11, 2026 at 8:18 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd want to hear various opinions how to safely use c++ in mission critical
I'm currently having fun trying to learn some of the Boost libary. I'm currently
I'm doing some file validation and want to load an UploadedFile into an external
I'm trying to write some NUnit tests for a project. I add a new
I developed a C# class library, some of their methods shows information of its
I have a C# class library which starts up a background consumer thread (lazily)
I'm raytracing and would like to speed it up via some acceleration structure (kd-tree,
So I have basically started documenting a class library I made, this is the
I would like to define properties for classes, and be able to access them
Like described in the title, is there some library in the Microsoft framework which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.