C# 尝试使用Ninject模块
这可能是一个愚蠢的问题,但我遵循这个教程 当我使用Ninject时,我有以下错误 名称“Bind”不存在 发生了什么事C# 尝试使用Ninject模块,c#,ninject,C#,Ninject,这可能是一个愚蠢的问题,但我遵循这个教程 当我使用Ninject时,我有以下错误 名称“Bind”不存在 发生了什么事 using Ninject.Modules; using Ninject; namespace WCFExampleLibrary.Services { public class IocServices : NinjectModule { public override void Load() { Bin
using Ninject.Modules;
using Ninject;
namespace WCFExampleLibrary.Services
{
public class IocServices : NinjectModule
{
public override void Load()
{
Bind<ICourseRepository>().To<CourseRepository>();
Bind<IStudentRepository>().To<StudentRepository>();
Bind<IEnrollementRepository>().To<EnrollementRepository>();
}
}
}
using System.Reflection;
using Ninject;
namespace WCFExampleLibrary.Services
{
public static class FactoryBuilder
{
private static IKernel _Kernal { get; set; }
public static T GetServices<T> ()
{
_Kernal = new StandardKernel();
_Kernal.Load(Assembly.GetExecutingAssembly());
return _Kernal.Get<T>();
}
}
}
使用Ninject.Modules;
使用Ninject;
命名空间WCFExampleLibrary.Services
{
公共类IocServices:NinjectModule
{
公共覆盖无效负载()
{
绑定()到();
绑定()到();
绑定()到();
}
}
}
运用系统反思;
使用Ninject;
命名空间WCFExampleLibrary.Services
{
公共静态类FactoryBuilder
{
私有静态IKernel _内核{get;set;}
公共静态GetServices()
{
_Kernal=新的标准内核();
_Load(Assembly.getExecutionGassembly());
return_Kernal.Get();
}
}
}
这里是一个简单的示例(您必须从NinjectModule继承):
类程序
{
静态void Main(字符串[]参数)
{
Ninject.IKernel kernel=新的标准内核(新的TestModule());
var samurai=kernel.Get();
武士。攻击(“你的敌人”);
Console.ReadLine();
}
}
公共接口IWeapon
{
弦击(弦靶);
}
公共级剑:IWeapon
{
公共字符串命中(字符串目标)
{
返回“切片”+目标+“对半”;
}
}
公共级匕首:IWeapon
{
公共字符串命中(字符串目标)
{
返回“刺+目标+死亡”;
}
}
公营武士
{
只读IWeapon[]所有武器;
公共武士(IWeapon[]所有武器)
{
这个。所有武器=所有武器;
}
公共无效攻击(字符串目标)
{
foreach(此中为IWeapon武器。所有武器)
控制台。写线(武器。命中(目标));
}
}
类TestModule:Ninject.Modules.NinjectModule
{
公共覆盖无效负载()
{
绑定()到();
绑定()到();
}
}
希望它对你有用。
你好,安迪你是怎么用的?需要查看更多代码。
class Program
{
static void Main(string[] args)
{
Ninject.IKernel kernel = new StandardKernel(new TestModule());
var samurai = kernel.Get<Samurai>();
samurai.Attack("your enemy");
Console.ReadLine();
}
}
public interface IWeapon
{
string Hit(string target);
}
public class Sword : IWeapon
{
public string Hit(string target)
{
return "Slice " + target + " in half";
}
}
public class Dagger : IWeapon
{
public string Hit(string target)
{
return "Stab " + target + " to death";
}
}
public class Samurai
{
readonly IWeapon[] allWeapons;
public Samurai(IWeapon[] allWeapons)
{
this.allWeapons = allWeapons;
}
public void Attack(string target)
{
foreach (IWeapon weapon in this.allWeapons)
Console.WriteLine(weapon.Hit(target));
}
}
class TestModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Dagger>();
}
}