0

I have seen information to programatically search all types within an assembly to identify which classes implement which interfaces or inherit from which base classes. But when you browse msdn (for example the Stream class) they present you with a nice Inheritance Hierarchy graph, and you can click to see what other classes inherit from it.

But the same functionality doesn't seem to exist for interfaces.

This is a general question. I can easily find all the classes that inherit from a specific parent class... But is there some way to do that with an Interface? Other than writing a program to search all the Assemblies? I'm looking for a way to search, for all classes that implement a particular interface...

Edward Ned Harvey
  • 4,901
  • 4
  • 27
  • 40
  • To solve your [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem): `new ReadOnlyCollection(colection1.Concat(collection2).ToList())`. Note that if `collection1` or `collection2` is modified it will not be reflected in the resultant collection. If you want it to reflect the changes you will need a custom class, there is nothing in the .NET framework that will do it for you. (if you don't understand how to make the class ask a new question and we can help you with that) – Scott Chamberlain Jan 02 '14 at 19:55
  • @ScottChamberlain Thank you, that answered the specific part of the question. I have removed the specific part of the question and just left the question as a general, "How do I search for which classes implement the ___ interface." – Edward Ned Harvey Jan 02 '14 at 20:26

4 Answers4

0

How do you find all implementations of an interface?

And to resolve your problem you can do

 IEnumerables.ToList()
Community
  • 1
  • 1
Pak
  • 2,009
  • 1
  • 18
  • 27
  • Thanks for the reference to that other question, but the answer is "buy resharper" ... which I don't think is a good answer (but good to know.) – Edward Ned Harvey Jan 02 '14 at 20:29
  • Or you can look at the second response wich is "You can right click a method name (definition in interface or implementation in other class) and choose View Call Hierarchy. In Call Hierarchy window there is "Implements" folder where you can find all locations of the interface method implementation." – Pak Jan 02 '14 at 22:55
  • Thanks again, but for example if I create a Stream object, and view call hierarchy on the Read method, I can only find places where something called Read. That's useful, and sometimes might help you find a class that implements Stream, that you didn't already know about, but by no means, does it search everything, unless you add reference to every Assembly in all of .Net. This is helpful, but definitely not a complete answer to the question. – Edward Ned Harvey Jan 03 '14 at 16:20
  • Oh - sorry - I forgot to mention. In my call hierarchy window, there is no "implements" folder. Only "Calls to Read" and "Calls from Read" and "Overrides Read" which might help you in some cases find what you're looking for in the current space. But you would have to add reference to every Assembly in .Net, for it to be a widely useful search. – Edward Ned Harvey Jan 03 '14 at 16:23
0

Code:

IEnumerables.ToList()

Google:

 ILIST C#  

You'll get links that have information like this:

[SerializableAttribute]  
[ComVisibleAttribute(true)]
public class ArrayList : IList, ICollection, 
    IEnumerable, ICloneable

You can see IList, also the bottom of the page has links to other lists.

T McKeown
  • 12,312
  • 1
  • 21
  • 30
  • @T McKeown What is IEnumerables? I don't see that anywhere. Also, when I google IList C#, I just come up with all the normal stuff I would expect, including the IList interface etc. What are you talking about? – Edward Ned Harvey Jan 02 '14 at 20:22
0

CTRL + ALT + J in Visual Studio will open the object browser.

Search for your type, e.g., 'IEnumerable'.

Expand the type. Expand 'Derived Types.'

This may not get you everything, but it's a place to start.

(at least VS 2012 PREMIUM)

ps2goat
  • 7,102
  • 1
  • 28
  • 59
  • I don't know if this is because I'm using VS 2013 PRO, or if it's because of something else, but I don't see what you're talking about. Similarly, your comment led me to find this: http://msdn.microsoft.com/en-us/library/w82x6b1s(v=vs.90).aspx which basically says the same thing as you, but still, I only see Base Types, not Derived Types. – Edward Ned Harvey Jan 02 '14 at 22:03
  • Yeah, I don't remember seeing it in previous pro versions either. It is a nice feature to have, though. I agree that there needs to be some easy way to query this, since there are some methods that require an interface and it's difficult to tell what default .net implementations are available. – ps2goat Jan 02 '14 at 22:30
0

Something like this:

  public static class TypeExtension
    {
        public static IEnumerable<Type> GetInterfaces(this Type type)
        {
            foreach (var intf in type.GetInterfaces())
            {
                yield return intf;

                foreach (var x in intf.GetInterfaces())
                    yield return x;
            }
        }
    }  
Val Bakhtin
  • 1,364
  • 8
  • 11