我试图编写一个返回实现接口(interface)的类列表的方法,但无法实现。
类似的方法
public List<Class> GetImplementedClasses(Interface Interface1)
{
. . .
}
我尝试使用 interface1.AllChildren 和许多其他尝试。他们都没有给出任何结果。
是否可以使用 DXCore API 编写这样的方法?
示例:
如果我传递 Interface1,我应该从方法 GetImplementedClasses 中获取 Class1 和 Class2。
提前致谢。
最佳答案
在 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;
}
关于coderush - 如何在 CodeRush 中获取实现接口(interface)的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539913/