如何在 Coderush 中获取类的祖先?

发布于 2024-11-08 14:38:51 字数 1290 浏览 0 评论 0原文

我试图将一个类的所有祖先(类和接口)放入列表中。

我尝试了这段代码,但没有成功。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞清仓 2024-11-15 14:38:51

有一种更好的方法来获取类的所有祖先:

ITypeElement[] ancestors = curClass.GetBaseTypes();

或者这个:

ITypeElement[] ancestors = CodeRush.Source.GetAllBaseTypes(curClass);

但是这些调用将返回 ITypeElement 类型的实例 - 这取决于您将如何使用祖先的实例。如果您想将 ITypeElement 转换为 Class 实例,请使用以下代码:

  foreach (ITypeElement ancestor in ancestors)
  {
    Class classInstance = ancestor.ToLanguageElement() as Class;
    if (classInstance != null)
      AncestorList.Add(classInstance);
  }

There's a better method to get all ancestors of a class:

ITypeElement[] ancestors = curClass.GetBaseTypes();

or this one:

ITypeElement[] ancestors = CodeRush.Source.GetAllBaseTypes(curClass);

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:

  foreach (ITypeElement ancestor in ancestors)
  {
    Class classInstance = ancestor.ToLanguageElement() as Class;
    if (classInstance != null)
      AncestorList.Add(classInstance);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文