4

I currently have a runtime type that I get through reflection. I need to create an instance of this type so I use the Activator class like so:

(T)Activator.CreateInstance(instance)

I use Autofac as my IoC and use DI throughout my app. Unfortunately I'm unable to use DI for these instances because that Activator class doesn't resolve dependencies when I try to inject them through the constructors.

Is there an Autofac method that will do this same thing but use my Autofac registrations to resolve the dependencies?

Thank you

1 Answer 1

4

I think you have a couple of options, knowing that, in general, Autofac needs to have the type inside the container in order to resolve it.

The easiest and most straightforward solution is to just register the type. If you have a reference to the Type at the time you're building your container, you can use that to register it with Autofac and resolve it.

Type customType = GetTheRuntimeType();
var builder = new ContainerBuilder();
builder.RegisterType(customType);

You can add more to that if you want (InstancePerLifetimeScope(), or As<IInterface>(), or whatever) but that'd work.

If you don't have the type at the time you're building the container then you could choose to manually inject properties after the fact. It's kind of like service location but it'd mean you could keep the type creation separate.

Type customType = GetTheRuntimeType();
var builder = new ContainerBuilder();
// register everything else, then
var container = builder.Build();
using(var scope = container.BeginLifetimeScope())
{
  var instance = Activator.CreateInstance(customType);
  scope.InjectUnsetProperties(instance);
}

It wouldn't be constructor injection, instead it'd be properties. But it's pretty easy.

The third option is the most integrated but possibly the hardest. You could implement a registration source that tells Autofac about your type. I'm not going to put the code here because there's a lot to it and I'm not at a compiler, but read the docs and check it out. There are examples in the docs. This is how Autofac dynamically handles things like IEnumerable<T> when you never explicitly registered that. You could come up with any set of runtime types you want and all of them could be dynamically inserted into the container using a registration source.

If you have the ability to do the first option and just register the type, I'd recommend that. It's the easiest way to do a good integration.

The property injection thing works but it's kind of a hack. Depends on how long lived your solution needs to be or if you just need to get it to work really quick.

The registration source solution is definitely the most robust. If you don't have the runtime type before you create the container then this is the best option. It's harder to get working at the outset but it'll be more extensible because you could piggyback any additional runtime type needs off of it.

Whatever you do, don't use Update() to add the registration after your container is built. It's been marked obsolete for a year or so now and is going to be coming out. It won't serve you as a long term solution.

3
  • I do know the types at compile time so I can just register those upfront. My question was about resolving runtime types. This is what I ended up doing: return (T)scope.Resolve(instance);
    – odannyc
    Jun 26, 2018 at 15:29
  • Sorry, I guess I misread the question. It sounded like you were having problems getting the type into Autofac. The solution you arrived at is absolutely how you'd get it back out once it's registered. Jun 26, 2018 at 15:39
  • Yeah, I was under the impression that Resolve only was a generic that took in a concrete type like so: Resolve<SomeClass>() but it turns out we can also resolve by giving it a type Resolve(runtimeType).
    – odannyc
    Jun 26, 2018 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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