C# 用Ninject注入一个整数
我有以下课程C# 用Ninject注入一个整数,c#,ninject,ninject-2,C#,Ninject,Ninject 2,我有以下课程 public class Foo { public Foo(int max=2000){...} } 我想使用Ninject向Foo中注入一个常量值。我试过这个 Bind<Foo>().ToSelft().WithConstructorArgument("max", 1000); 以下内容适合我: using System; using System.Collections.Generic; using System.Linq; using System
public class Foo
{
public Foo(int max=2000){...}
}
我想使用Ninject向Foo中注入一个常量值。我试过这个
Bind<Foo>().ToSelft().WithConstructorArgument("max", 1000);
以下内容适合我:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject;
using Ninject.Activation;
using Ninject.Syntax;
public class Foo
{
public int TestProperty { get; set; }
public Foo(int max = 2000)
{
TestProperty = max;
}
}
public class Program
{
public static void Main(string [] arg)
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<Foo>().ToSelf().WithConstructorArgument("max", 1000);
var foo = kernel.Get<Foo>();
Console.WriteLine(foo.TestProperty); // 1000
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Ninject;
使用Ninject.Activation;
使用Ninject.Syntax;
公开课Foo
{
公共int TestProperty{get;set;}
公共Foo(整数最大值=2000)
{
TestProperty=max;
}
}
公共课程
{
公共静态void Main(字符串[]arg)
{
使用(IKernel kernel=new-StandardKernel())
{
kernel.Bind().ToSelf().WithConstructorArgument(“max”,1000);
var foo=kernel.Get();
Console.WriteLine(foo.TestProperty);//1000
}
}
}
奇怪。我一定是在密码里做了什么错事。感谢您的帮助您使用自绑定的具体原因是什么?我刚试过这个,我也犯了同样的错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject;
using Ninject.Activation;
using Ninject.Syntax;
public class Foo
{
public int TestProperty { get; set; }
public Foo(int max = 2000)
{
TestProperty = max;
}
}
public class Program
{
public static void Main(string [] arg)
{
using (IKernel kernel = new StandardKernel())
{
kernel.Bind<Foo>().ToSelf().WithConstructorArgument("max", 1000);
var foo = kernel.Get<Foo>();
Console.WriteLine(foo.TestProperty); // 1000
}
}
}