Query Unity for all interface instances

I need to find out in my project all the classes that implement the IMyInterface interface and are registered in the Unity container.

Does anyone know a way to do this without having unity, instantiate objects registered in Unity?

+4
source share
2 answers

You can request registration in a container instance using a query like this

var x = container.Registrations.Where(cm => cm.RegisteredType == typeof(IMyInterface)); 
+4
source

You can first query for all types that are not abstract and implement your interface:

 this.GetType().Assembly.GetTypes().Where(type => type.IsClass && !type.IsAbstract && type.IsAssignableFrom(IMyInterface)); 

Then, for each of these types, query your Unity container to see if it contains an instance of it.

0
source

Source: https://habr.com/ru/post/1392324/


All Articles