Is it possible to get the complete file path of a class where the source code is located using coderush APIs?

131 Views Asked by At

I wanted to get the full file path when the caret in visual studio is at object Creation or referring a method of some other class.

Something like

Class CurrentClass
{
   Class2 object1=new Class2();

   object1.method1();

}

Can I get the complete file path like c:\ProjectLocation\Class2.cs.

When I get this line in visual studio.

Class2 object1=new Class2();
1

There are 1 best solutions below

2
On BEST ANSWER

You can resolve the active expression (object creation expression, type reference expression, method reference expression), and get the file name with resolved declaration, using code like this:

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