Введите целое число с помощью Ninject - PullRequest
2 голосов
/ 27 мая 2011

У меня есть следующий класс

public class Foo
{
  public Foo(int max=2000){...}
}

и я хочу использовать Ninject, чтобы ввести постоянное значение в Foo. Я должен попробовать это

Bind<Foo>().ToSelft().WithConstructorArgument("max", 1000);

но я получаю следующую ошибку при попытке использовать _ninject.Get<Foo>:

Error activating int
No matching bindings are available, and the type is not self-bindable.
Activation path:
  3) Injection of dependency int into parameter max of constructor of type Foo

1 Ответ

6 голосов
/ 27 мая 2011

у меня работает ниже:

    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
              }

        }
    }
...