coderushdxcore

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


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.


Solution

  • 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;
    }