如何在 Coderush 中获取类的祖先?
我试图将一个类的所有祖先(类和接口)放入列表中。
我尝试了这段代码,但没有成功。
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);
提前致谢,
维诺德
I was trying to get all ancestors (Classes and Interfaces) of a Class to a list.
I tried this code but did not work.
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.
}
}
}
In this part of code I tried to collect all the classes and interfaces into AncestorList.
But when I tried to find number classes in the list it showed 0. The test project had few parent classes and interfaces. Please help to find the error.
I made a Call to the GetAncestorList function in the following way.
AncestorList=new List<Class>();
GetAncestorList(currentClass);
Thanks in Advance,
Vinod
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一种更好的方法来获取类的所有祖先:
或者这个:
但是这些调用将返回 ITypeElement 类型的实例 - 这取决于您将如何使用祖先的实例。如果您想将 ITypeElement 转换为 Class 实例,请使用以下代码:
There's a better method to get all ancestors of a class:
or this one:
But these calls will return instances of ITypeElement type - it depends on how you are going to use instances of ancestors. If you'd like to convert ITypeElement into a Class instance, use the following code: