简体   繁体   中英

Autofac Resolve all registered instances by runtime provided Type

Given a type provided at runtime such as:

var type = typeof(IMyType);

Does Autofac have the ability to Resolve all registered instances of that type? eg I'm looking for something like this (without Generics):

container.ResolveMany(type);

Yes, you can resolve a collection of that type.

Something like this:

var type = typeof(IMyType);
var collectionType = typeof(IReadOnlyCollection<>).MakeGenericType(type);
var resolvedTypes = container.Resolve(collectionType);

Working example

However, because type is only known at runtime, to do anything with the resolved instances you would need to use reflection. The only compile time cast that could be performed would be to IEnumerable<object> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM