locked
How to get the name of a using Namespace from codemodel RRS feed

  • Question

  • I have a codeElement of the using statement but Name throws COMException.  The same call on VB returns the name.  How can I get the name of the imported namespace?

    In VB "Import System.IO" codeElement.Name returns System.IO, in C# "using System.IO;" codeElement.Name throws.

    Thanks,
    Erik

    Wednesday, April 21, 2010 1:06 AM

Answers

  • CodeImport.Namespace will give you the imported namespace, and CodeImport.Alias will give you the alias if there's one, empty string otherwise. This works for both C# and VB. Thanks!


    Frank Fan [MSFT]
    • Marked as answer by ECutts Friday, April 23, 2010 10:52 PM
    Friday, April 23, 2010 10:42 PM

All replies

  • Halo, Eric

    The reason may be the automation doesn’t implement this property, to get the name of imported using statement. We could parse it ourselves.

    First, we must ensure that the Project item we want to get the code element is open.

    Then, please reference the following code that get the imported using statement.

                        foreach(CodeElement2 ce in ces)

                        {

                           if(ce.Kind == vsCMElement.vsCMElementImportStmt)

                           {

                               CodeImport cImport = ce as CodeImport;

                               EditPoint editPoint = cImport.StartPoint.CreateEditPoint();

                               // Get the using statement.

                               String txt = editPoint.GetText(cImport.EndPoint);

                               string[] strs = txt.Split(new char[2]{' ',';'});

                               string importName = strs[1];

                           }

                        }

    Thanks

    Chao

    Thursday, April 22, 2010 5:22 AM
  • CodeImport.Namespace will give you the imported namespace, and CodeImport.Alias will give you the alias if there's one, empty string otherwise. This works for both C# and VB. Thanks!


    Frank Fan [MSFT]
    • Marked as answer by ECutts Friday, April 23, 2010 10:52 PM
    Friday, April 23, 2010 10:42 PM