Skip to content

Instantly share code, notes, and snippets.

@xcap2000
Created October 9, 2019 01:29
Show Gist options
  • Save xcap2000/2d264960cae55cf131edaa907a1a3eae to your computer and use it in GitHub Desktop.
Save xcap2000/2d264960cae55cf131edaa907a1a3eae to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using Unity;
using Unity.Resolution;
namespace MlWebUi.Common.Resolvers
{
public class Resolver<T> : IResolver<T>
{
private readonly IUnityContainer container;
private readonly string? name;
public Resolver(IUnityContainer container, string? name = null)
{
this.container = container;
this.name = name;
}
public T Resolve()
{
return container.Resolve<T>(name);
}
public T Resolve(params object[] arguments)
{
var overrides = new List<ResolverOverride>();
foreach (var argument in arguments)
{
overrides.Add(Override.Parameter(argument.GetType(), argument));
}
return container.Resolve<T>(name, overrides.ToArray());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment