I
I
i__egor2021-06-21 18:53:27
C++ / C#
i__egor, 2021-06-21 18:53:27

How can I get a list of interfaces of only inheritors of a certain base interface from a Type instance?

Let's say there is a basic interface IBaseIntarface and several interfaces inheriting it IMyIntarface1, IMyIntarface2 and so on.
There is a class that implements these heirs (maybe several of them), plus there may be some more

//IBaseIntarface base_intarface;
Type type = base_intarface.GetType();
 List<Type> list_types = type.GetInterfaces().ToList();

In this case, all interfaces get into list_types (plus IBaseIntarface for some reason (I think if it still had a parent, it would also get there))
If I do this:
List<Type> list_types = type
                .GetInterfaces()
                .Where(it => it != typeof(IBaseIntarface))
                .ToList();

Then the base interface is no longer included in the list. but there is still everything that the object has.
If you write like this:
List<Type> list_types = type
                .GetInterfaces()
                .Where(it => it != typeof(IBaseIntarface) && it is IBaseIntarface)
                .ToList();

That won't make it to the list.
How to write a condition so that the list contains only child interfaces from a certain base?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kachan, 2021-06-21
@MANAB

https://stackoverflow.com/questions/26733/getting-...
The approach is to go through all the types in the assembly and see if they implement the given interface.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question