0

Given the following interfaces and classes, is there a way in Autofac to

  • Register a Provider<T> for all classes that have a ProviderAttribute, with T being the type of such a class (Think registering open generics and resolving them with Autofac's MakeGenericType())
  • Inject these registered providers as, drum roll, IEnumerable<IProviderBase> into the constructor of other classes

Overview:

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 Answer 1

0

I have come up with a crude solution:

var types = GetProviderTypes();

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.