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 5945259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:39:22+00:00 2026-05-22T16:39:22+00:00

Code Sample: namespace DependencyInjection { class Program { static void Main(string[] args) { IKernel

  • 0

Code Sample:

namespace DependencyInjection
{
class Program  
{
    static void Main(string[] args)
    {
        IKernel kernel = new StandardKernel();
        var samurai = kernel.Get<Samurai>();
        Bind<IWeapon>().To<Sword>();
   }       
}



class Samurai
{
    readonly IWeapon _weapon;
    public Samurai(IWeapon weapon)
    {
        _weapon = weapon;
    }
    public void Attack(string target)
    {
        _weapon.Hit(target);
    }
}

interface IWeapon
{
    void Hit(string target);
}

class Sword : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Sword - ", target);
    }
}

class Arrow : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Arrow - ", target);
    }
}
}

This is my first attempt to implement DI using ninject.

I am not sure how to resolve the error “The name ‘Bind’ does not exist in the current context”. I went through this question Compilation Error with Ninject but still not sure how to solve this. How can this be resolved. It will be great if I can get the code sample so that I can understand much better

Edit :

namespace DependencyInjection
{
class Program  
{
    static void Main(string[] args)
    {
        IKernel kernel = new StandardKernel();
        kernel.Bind<IWeapon>().To<Sword>();
        var samurai = kernel.Get<Samurai>(); -----> Exception Line
        samurai.Attack("Hello");
   }       
}

class Samurai
{
    readonly IWeapon _weapon;
    public Samurai(IWeapon weapon)
    {
        _weapon = weapon;
    }
    public void Attack(string target)
    {
        _weapon.Hit(target);
    }
}

interface IWeapon
{
    void Hit(string target);
}

class Sword : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Sword - ", target);
    }
}

class Arrow : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Arrow - ", target);
    }
}
}

The above code results in “MethodAccessException was unhandled” on line var samurai = kernel.Get(); I search Google but was not able to find any concrete solution

Exception

System.MethodAccessException was unhandled
  Message="DependencyInjection.Sword..ctor()"
  Source="Anonymously Hosted DynamicMethods Assembly"
  StackTrace:
       at DynamicInjector84db385a6cfb4301b146100b5027c44a(Object[] )
       at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
       at Ninject.Activation.Context.Resolve()
       at Ninject.KernelBase.<Resolve>b__4(IContext context)
       at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
       at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
       at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass1.<Create>b__0(ITarget target)
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
       at Ninject.Activation.Context.Resolve()
       at Ninject.KernelBase.<Resolve>b__4(IContext context)
       at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.<CastIterator>d__aa`1.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
       at DependencyInjection.Program.Main(String[] args) in D:\Sandboxes\C_Sharp\DependencyInjection\DependencyInjection\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
  • 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-22T16:39:22+00:00Added an answer on May 22, 2026 at 4:39 pm

    The reason why you are getting this error is because Bind is normally a method on the kernel, so you should do this outside the module. Also you should create your bindings before resolving Samurai, so swap the last two lines around:

    static void Main(string[] args)
    {
        IKernel kernel = new StandardKernel();
        kernel.Bind<IWeapon>().To<Sword>();
        kernel.Bind<Samurai>().ToSelf();
        var samurai = kernel.Get<Samurai>();
    

    }

    The reason why Bind sometimes works without kernel is because you probably saw tutorials doing that inside a module. NinjectModule has a protected Bind method that performs the same function:

    public class NinjaModule : NinjectModule {
        public void Load() {
           Bind<Samurai>().ToSelf()
           Bind<IWeapon>().To<Sword>();
        }
    } 
    

    Either method is appropriate, when your bindings get a bit complex, it’s advisable to put them into modules.

    EDIT Make sure all of your classes and interfaces are public, otherwise Ninject can’t execute them. When accessibility modifier is not specified, it defaults to internal. This explains MethodAccessException.

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

Sidebar

Related Questions

Here's some sample code: using System; namespace UnloadFromFinalizer { class Program { static void
Consider the following sample code below: #include <iostream> using namespace std; class base {
Conside the following sample code below: #include <iostream> using namespace std; class base {
Consider the following sample code: class Base { public: void f(); virtual void vf();
Consider the sample program below: #include <iostream> using namespace std; class test { public:
Consider the sample code below: #include <iostream> using namespace std; class A { private:
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
Can anybody help me with this simple code?? #include <iostream> using namespace std; void
I have a small code sample: private void MonitorItems() { if (someCondition) { dateSelected
I have a sample code : #include <iostream> #include <conio.h> using namespace std; int

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.