From 2b826a82950db7dc7736c6b10f9573b3f12ccfe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sun, 29 Nov 2020 13:51:19 +0100 Subject: [PATCH] Instantiating through autofac but still choose which instance to use --- AutoFacSamles/Program.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/AutoFacSamles/Program.cs b/AutoFacSamles/Program.cs index 60ed079..eea8a5b 100644 --- a/AutoFacSamles/Program.cs +++ b/AutoFacSamles/Program.cs @@ -29,6 +29,13 @@ namespace AutoFacSamles { private ILog log; private int id; + + public Engine(ILog log, int id) + { + this.log = log; + this.id = id; + } + public Engine(ILog log) { this.log = log; @@ -70,13 +77,13 @@ namespace AutoFacSamles static void Main(string[] args) { var builder = new ContainerBuilder(); - //builder.RegisterType().As(); - var log = new ConsoleLog(); - builder.RegisterInstance(log).As(); //val av redan instantierat objekt + builder.RegisterType().As(); - builder.RegisterType(); - builder.RegisterType() - .UsingConstructor(typeof(Engine)); //val av contructor + builder.Register( c => + new Engine(c.Resolve(), 123)); + + //builder.RegisterType(); + builder.RegisterType(); IContainer container = builder.Build();