2011-06-30 28 views
3

我正在尝试编写一个方法,它返回一个实现接口的类的列表,但不能这样做。如何获得在CodeRush中实现接口的类?

一些方法像

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

我试图用interface1.AllChildren等诸多尝试。他们中没有人给出任何结果。

是否可以使用DXCore API编写这样的方法?

例:

enter image description here

如果我通过接口1,我应该从方法GetImplementedClasses得到的Class1和Class2中。

在此先感谢。

回答

4

Intrerface类中有一个“GetDescendants”方法,可能对您的任务有用。该代码将类似于此:

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; 
} 
+0

谢谢你的答案..但是这不工作... ITypeElement [] descendants = Interface1.GetDescendants();在这一行Interface1.GetDescendants()返回null .. – Sachin

+1

奇怪的是,我创建了一个测试插件,它显示了接口的所有实现者,并且它按预期工作。你如何检索一个接口的实例?您可以从此链接下载测试插件和测试代码(根据您的示例):http://downloads.devexpress.com/Share/Temp/TestPlugIn/GetImplementedClasses.zip –

+0

这没有奏效。我在CodeRushXpress-10.2.6和CodeRushXpress-11.1.4上试过这个。我也在[visual studio外的控制台应用程序]中尝试了这一点(http://www.skorkin.com/2011/06/how-to-use-dxcore-in-a-console-app-outside-of-visual-工作室/)和关于后代的手表显示为空。 – Sachin

1

我不知道dxcore,但在普通的C#我已经写an extension method它得到所有部署中可用的类型;你可以像这样使用它:

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

谢谢你的答案..nice的博客... – Sachin

+0

我想写一些简单的插件到Visual Studio,所以用CodeRush API回答更接近这个问题.... :) – Sachin

+0

够公平:-) –

相关问题