Autofac:打开泛型和基本接口

时间:2013-10-31 14:00:43

标签: autofac open-generics

鉴于以下接口和类,Autofac中是否有一种方法

  • 为所有具有Provider<T>的课程注册ProviderAttribute,其中T是此类的类型(想想注册开放式泛型并使用Autofac的MakeGenericType()解析它们)
  • 将这些注册的提供者注册为鼓,IEnumerable<IProviderBase>到其他类的构造函数

概述:

public class ProviderAttribute : Attribute { }

public interface IProviderBase
{
    Type Type { get; }
}

public interface IProvider<T> : IProviderBase
{
    DoSomething(T t);
}

public class Provider<T> : IProvider<T>
{
    public Type Type
    {
        get { return typeof (T); }
    }

    public DoSomething(T t)
    {
        //...
    }
}

1 个答案:

答案 0 :(得分:0)

我想出了一个粗糙的解决方案:

var types = GetProviderTypes();

foreach (var type in types)
{
    var t = typeof (Provider<>).MakeGenericType(type);
    builder.RegisterType(t).As<IProviderBase>();
}
相关问题