Created
November 8, 2020 00:37
-
-
Save emanuelefirmani/6e766f8cced16358ec4fae412d27a1c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Autofac; | |
using FluentAssertions; | |
using Xunit; | |
namespace GenericLogger | |
{ | |
public interface ILogger<T> { } | |
public class Logger<T> : ILogger<T> | |
{ | |
public string Parameter { get; } | |
public Logger(string parameter) | |
{ | |
Parameter = parameter; | |
} | |
} | |
class MyClass | |
{ | |
public ILogger<int> Logger { get; } | |
public MyClass(ILogger<int> logger) | |
{ | |
Logger = logger; | |
} | |
} | |
public class UnitTest1 | |
{ | |
private readonly ILifetimeScope _lifetimeScope; | |
public UnitTest1() | |
{ | |
var cb = new ContainerBuilder(); | |
cb.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>)); | |
cb.Register(c => new MyClass(c.Resolve<ILogger<int>>(new NamedParameter("parameter", "param1")))); | |
var container = cb.Build(); | |
_lifetimeScope = container.BeginLifetimeScope(); | |
} | |
[Fact] | |
public void Test1() | |
{ | |
var actual = _lifetimeScope.Resolve<MyClass>(); | |
((Logger<int>) actual.Logger).Parameter.Should().Be("param1"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment