How to get the classes that implement an interface in CodeRush?

234 views Asked by At

I was trying to write a method that returns a list of classes that implements an Interface, but could not do it.

Some method like

  public List<Class> GetImplementedClasses(Interface Interface1)
   {
        . . . 
   }

I tried to use interface1.AllChildren and many other tries. Non of them gave any results.

Is it possible to write such a method using DXCore APIs?

EXAMPLE:

enter image description here

If I pass Interface1, I should get Class1 and Class2 from the method GetImplementedClasses.

Thanks in Advance.

2

There are 2 answers

7
Alex Skorkin On BEST ANSWER

There's a "GetDescendants" method in the Intrerface class that might be useful for your task. The code will look similar to this:

public List<Class> GetImplementedClasses(Interface Interface1)
{
  List<Class> result = new List<Class>();
  if (Interface1 != null)
  {
    ITypeElement[] descendants = Interface1.GetDescendants();
    foreach (ITypeElement descendant in descendants)
    {
      if (!descendant.InReferencedAssembly)
      {
        Class classDescendant = descendant.ToLanguageElement() as Class;
        if (classDescendant != null)
          result.Add(classDescendant);
      }
    }
  }
  return result;
}
1
Steve Wilkes On

I'm not sure about dxcore, but in regular C# I've written an extension method which gets all the Types available in a deployment; you could use it like this:

Assembly.GetExecutingAssembly().GetAvailableTypes(
    typeFilter: t => 
        (t != typeof(interfaceType)) 
        && 
        typeof(interfaceType).IsAssignableFrom(t));