Skip to content

Instantly share code, notes, and snippets.

@emanuelefirmani
Last active November 8, 2020 01:10
Show Gist options
  • Save emanuelefirmani/b9642317bceb91511d6574f9a5391dbb to your computer and use it in GitHub Desktop.
Save emanuelefirmani/b9642317bceb91511d6574f9a5391dbb to your computer and use it in GitHub Desktop.
using Autofac;
using FluentAssertions;
using Xunit;
namespace GenericLogger
{
public interface ILogger<T>
{
delegate ILogger<T> Factory(MyParameter parameter);
}
public class Logger<T> : ILogger<T>
{
public MyParameter Parameter { get; }
public Logger(MyParameter parameter)
{
Parameter = parameter;
}
}
public class MyParameter {
public string Value { get; set; } }
class MyClass
{
public ILogger<string> Logger { get; }
public MyClass(ILogger<string>.Factory loggerFactory)
{
Logger = loggerFactory.Invoke(new MyParameter { Value = "a"});
}
}
public class UnitTest1
{
private readonly ILifetimeScope _lifetimeScope;
public UnitTest1()
{
var cb = new ContainerBuilder();
cb.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>));
cb.RegisterType<MyClass>();
var container = cb.Build();
_lifetimeScope = container.BeginLifetimeScope();
}
[Fact]
public void Test1()
{
var actual = _lifetimeScope.Resolve<MyClass>();
((Logger<string>)actual.Logger).Parameter.Value.Should().Be("a");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment