我试图将一个类的所有祖先(类和接口)放到一个列表中。
我尝试了这段代码,但没有奏效。
public static List<Class> AncestorList;
public static void GetAncestorList(Class curClass)
{
if(curClass.PrimaryAncestorType!=null)
{
Class primaryAncestor = curClass.PrimaryAncestorType.Resolve(new SourceTreeResolver()) as Class;
if(primaryAncestor !=null)
{
AncestorList.Add(primaryAncestor);//Add primary ancestor to the list.
GetAncestorList(primaryAncestor);//find the ancestors of the primary ancestor.
}
foreach (TypeReferenceExpression typ1 in curClass.SecondaryAncestorTypes )
{
Class secAncestor = typ1.Resolve(new SourceTreeResolver()) as Class;
if(secAncestor !=null)
{
AncestorList.Add(secAncestor);//Add secondary ancestor to the list.
GetAncestorList(secAncestor);//find ancestors of secondary ancestor.
}
}
}
在这部分代码中,我尝试将所有类和接口收集到 AncestorList 中。但是当我试图在列表中找到数字类时,它显示为 0。测试项目的父类和接口很少。请帮助查找错误。
我通过以下方式调用了 GetAncestorList 函数。
AncestorList=new List<Class>();
GetAncestorList(currentClass);
提前致谢,
维诺德