【问题标题】:Is it possible to get the complete file path of a class where the source code is located using coderush APIs?是否可以使用 coderush API 获取源代码所在类的完整文件路径?
【发布时间】:2011-05-02 13:12:23
【问题描述】:

当 Visual Studio 中的插入符号位于对象创建或引用其他类的方法时,我想获取完整的文件路径。

类似

Class CurrentClass
{
   Class2 object1=new Class2();

   object1.method1();

}

我可以获取完整的文件路径,如 c:\ProjectLocation\Class2.cs

当我在 Visual Studio 中得到这条线时。

Class2 object1=new Class2();

【问题讨论】:

    标签: visual-studio coderush dxcore


    【解决方案1】:

    您可以解析活动表达式(对象创建表达式、类型引用表达式、方法引用表达式),并获取带有已解析声明的文件名,使用如下代码:

      Expression activeExpression = CodeRush.Source.Active as Expression;
      if (activeExpression!= null)
      {
        IElement declaration = activeExpression.Resolve(new SourceTreeResolver());
        if (declaration != null)
        {
          string fileName = declaration.FirstFile.Name;
          // use the fileName...
        }
      }
    

    【讨论】:

    • 我只是想用 coderush 做一些基本的事情,比如在父类中获取所有方法......我尝试了 Class parent= declaraton.GetDeclaration() as Class 并尝试使用 parent.AllMethods,这并没有提供对所有方法的访问权限那个类....我正在尝试使用 TypeReferenceExpression 是否还有其他一些我需要查看的类或函数??
    • 'declaration' 在我们的代码中应该是一个 ITypeElement 类型的实例,所以尝试使用这样的代码: ITypeElement typeInstance = declaration as ITypeElement; if (typeInstance != null) foreach (IMemberElement member in typeInstance.Members) { IMethodElement method = member as IMethodElement; if (method != null) { // 用方法做某事... } }
    猜你喜欢
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多