2011-05-02 18 views
1

当Visual Studio中的插入符号位于对象创建或引用某个其他类的方法时,我想获得完整的文件路径。是否有可能使用coderush API获取源代码所在类的完整文件路径?

喜欢的东西

Class CurrentClass 
{ 
    Class2 object1=new Class2(); 

    object1.method1(); 

} 

我能得到这样c中的完整的文件路径:\ ProjectLocation \ Class2.cs

当我在visual studio中得到这一行。

Class2 object1=new Class2(); 

回答

2

可以解决当前表达式(对象创建表达式,类型参考表达,方法参考表达),并与解决申报获取文件名,使用这样的代码:

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... 
    } 
    } 
+0

谢谢亚历克斯...这工作正常.. :) – Sachin 2011-05-17 13:24:43

+0

我只是想与coderush做一些基本的东西,像获得父类中的所有方法...并且我尝试**类** **父= ** ** declaraton。 GetDeclaration()** **作为** **类**并试图使用** parent.AllMethods **,这并没有给予该类中所有方法的访问权限....我试着用** TypeReferenceExpression **有没有她需要看的类或函数? – Sachin 2011-05-20 12:28:37

+1

我们的代码中的'声明'应该是ITypeElement类型的实例,因此请尝试使用如下代码: ITypeElement typeInstance =声明为ITypeElement; if(typeInstance!= null) foreach(typeInstance.Members中的IMemberElement成员) IMethodElement method = member as IMethodElement; if(method!= null) { // do something something method ... } } – 2011-05-20 16:04:36

相关问题