Ninject 编译错误 [英] Compilation Error with Ninject

查看:87
本文介绍了Ninject 编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ninject 看起来很棒,所以我想在我的项目中使用它.不幸的是,我仍在努力做最琐碎的绑定.[Inject] 属性编译得很好,但是编译器找不到绑定"命令:

Ninject looks great, so I'd like to use it in my project. Unfortunately I am still struggling to do the most trivial binding. The [Inject] attribute compiles just fine, but the compiler cannot find the "Bind" command:

using System;
using Ninject.Core;
using Ninject.Core.Binding;

namespace NinjectTest
{
    public interface IFoo
    {
        void DoSomething();
    }

    public class Foo : IFoo
    {
        public void DoSomething()
        {
            throw new NotImplementedException();
        }
    }

    public class Bar
    {
        [Inject] private IFoo theFoo;

        public Bar()
        {
            Bind<IFoo>().To<Foo>(); //Compiler Error: "The name 'Bind' does not exist in the current context"
        }
    }
}

这里可能出了什么问题?

What could be going wrong here?

推荐答案

Bind 方法是 Ninject StandardModule 类中的一个方法.您需要继承该类才能绑定.

The Bind method is a method in the Ninject StandardModule class. You need to inherit that class to be able to bind.

这是一个简单的例子:

using System; 
using System.Collections.Generic; 
using System.Text; 
using Ninject.Core;

namespace Forecast.Domain.Implementation 
{
    public class NinjectBaseModule : StandardModule
    {
        public override void Load()
        {
            Bind<ICountStocks>().To<Holding>();
            Bind<IOwn>().To<Portfolio>();
            Bind<ICountMoney>().To<Wallet>();
        }
    } 
}

这篇关于Ninject 编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆