Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using Ninject;
  2. using Ninject.Activation;
  3. using Ninject.Extensions.Factory;
  4. using Ninject.Planning.Targets;
  5. using System;
  6. using System.Linq;
  7.  
  8. namespace DI
  9. {
  10.     public interface INumberFactory
  11.     {
  12.         Test Create(int a);
  13.     }
  14.  
  15.     public class Test
  16.     {
  17.         public Test(int a) { }
  18.     }
  19.  
  20.     class Program
  21.     {
  22.         static void Main(string[] args)
  23.         {
  24.             var kernel = new StandardKernel(new FuncModule());
  25.             kernel.Bind<Test>().ToSelf().WithConstructorArgument("a" , (context, target) =>
  26.             {
  27.                 Console.WriteLine("I'm trying to retrieve the constructor argument");
  28.  
  29.                 return 0;
  30.             });
  31.             kernel.Bind<INumberFactory>().ToFactory();
  32.  
  33.             // Nothing is printed, even if it logically should be
  34.             kernel.Get<INumberFactory>().Create(1);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement