我想做类似以下的事情:
public class LightInjectTests
{
[Fact]
public void CanInjectItemIntoScope()
{
var container = new ServiceContainer();
container.Register<IService, Service>(new PerScopeLifetime());
var txId = Guid.NewGuid();
var context = new Context(txId);
using (var scope = container.BeginScope())
{
scope.Inject<IContext>(context);
var service = scope.GetInstance<IService>();
service.Context.Should().BeSameAs(context);
}
}
interface IContext { }
class Context : IContext
{
public Context(Guid txId) => TxId = txId;
public Guid TxId { get; }
}
interface IService
{
IContext Context { get; }
}
class Service : IService
{
public Service(IContext context) => Context = context;
public IContext Context { get; }
}
}
这可能吗?我该怎么办?