3

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.

Community
  • 1
  • 1
Sachin
  • 155
  • 1
  • 8

2 Answers2

4

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;
}
Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
  • Thank you for the answer.. but this is not working ...ITypeElement[] descendants = Interface1.GetDescendants(); in this line Interface1.GetDescendants() returns null.. – Sachin Jul 01 '11 at 08:35
  • 1
    Strange, I've created a test plug-in that shows all implementors of an Interface and it works as expected. How do you retrieve an instance of an Interface? You can download the test plug-in and test code (according to your example) from this link: http://downloads.devexpress.com/Share/Temp/TestPlugIn/GetImplementedClasses.zip – Alex Skorkin Jul 01 '11 at 11:00
  • This did not work. I tried this with CodeRushXpress-10.2.6 and CodeRushXpress-11.1.4. I also tried this in the [console app outside visual studio](http://www.skorkin.com/2011/06/how-to-use-dxcore-in-a-console-app-outside-of-visual-studio/) and a watch on descendants showed null. – Sachin Jul 04 '11 at 08:34
  • @Sachin, Could you please share your plug-in for investigation? You may contact DevExpress Support Services at "support @ devexpress . com" for doing this, they will be happy to help you. – Alex Skorkin Jul 04 '11 at 09:36
  • I have just mailed the problem to devexpress support. Thanks. – Sachin Jul 04 '11 at 15:27
  • is there any other way in which i can get all the descendants of an interface? It would be helpful if there is an alternative – Sachin Aug 02 '11 at 17:56
  • 1
    Yes, try this: DescendantReferencesSearcher searcher = new DescendantReferencesSearcher(ParserServices.SourceTreeResolver); IElementCollection descendants = searcher.Search(interface1.Solution, type); – Alex Skorkin Aug 03 '11 at 13:45
1

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));
Steve Wilkes
  • 7,085
  • 3
  • 29
  • 32
  • I am trying to write some simple plugins to Visual studio, so answer with CodeRush APIs is more near to this question ....:) – Sachin Jul 01 '11 at 08:44