Autofac:使用动态类型参数解析泛型的工厂

时间:2017-01-07 06:32:23

标签: c# .net dependency-injection autofac

这是理念。

public interface IGeneralInterface { }
public interface IGenericInterface<T> : IGeneralInterface { }
public class GenericClass<T> : IGenericInterface<T> { }

public class SomeClient
{
    Func<Type, IGeneralInterface> serviceFactory;

    public SomeClient(Func<Type, IGeneralInterface> serviceFactory)
    {
        this.serviceFactory = serviceFactory;
    }
    public void DoSomething<T>()
    {
        var service = serviceFactory(typeof(T));
    }
}

使用Autofac,如何将此服务工厂注入客户端,以便在调用client.DoSomething()时可以解决正确的服务?

0 个答案:

没有答案