locked
FileCodeModel null for file in "Misc Files" project. RRS feed

  • Question

  •  

    Hi!

    I'm creating a program that looks at source files and performs certain checks on them. This is all done at the source file level (i.e. there is no reference to a project or solution). I'm adding functionality so that when a user double clicks on a node in a GUI (where there is one for every method and property in a file) it loads up visual studio and highlights that method or property.

     

    Before launching a new Visual Studio, I check all the open visual studios to see if they have a Solution which already contains this file and if so perform all the logic on that. This works great as the source file has already be parsed and has a lovely FileCodeModel for me to iterate through and fine the method (complete with start and end points!).

     

    The problem comes when there a) isnt a visual studio instance open or b) the file being checked doesnt belong to an open project. In this situations the FileCodeModel is null. Now when I check the project for this its called "Miscellaneous Files" or something similar; so I assume when ever you open just a single file in Visual Studio it goes in here.

     

    What I don't understand is why can't I access the member collection for this file? Obviously Visual Studio does have the information for it, because all the members / classes / enums etc are in the drop downs at the top of the file (in the IDE). Is there another way to access this information or force Visual Studio to create a FileCodeModel for this? The only (hack) solution I can think of is creating a new Visual Studio process, where I create a new solution and project in this to force it to make a model. This how ever will be expencive; I'm sure the information is already stored somewhere!

     

    Thanks!

    (Hopefully this is the right forum!)

    Sunday, September 23, 2007 8:06 PM

Answers

  •  

    The project system is responsible for creating instances of a CodeMode/FileCodeModel. To construct a code model, the code must be compiled, then the internal representation of that compiled code needs to be translated into the objects necessary to construct a code model. The Misc Files project is just a Solution Folder with a special name. Solution Folders are containers, and do not have any special knowledge of the files contained within them. They do not compile the files as you can put png, txt, cs, vb, cpp, or files for a language released after Visual Studio ships into a Solution Folder, and the solution folder would not know how to compile those files. For example, there is no mechanism for using conditional compilation in a solution folder, what would be the correct way to compile a C# file that contains conditionals?

     

    Unless a file is contained within a project that was designed to compile that file type - a .cs file in a C# project, a .cpp file in a C++ project, a .vb file in a VB project - then trying to retrieve a code model is not possible. For solution folders we always return NULL when the CodeModel or FileCodeModel properties are called.

     

    Craig

    Tuesday, September 25, 2007 12:45 AM

All replies

  •  

    The project system is responsible for creating instances of a CodeMode/FileCodeModel. To construct a code model, the code must be compiled, then the internal representation of that compiled code needs to be translated into the objects necessary to construct a code model. The Misc Files project is just a Solution Folder with a special name. Solution Folders are containers, and do not have any special knowledge of the files contained within them. They do not compile the files as you can put png, txt, cs, vb, cpp, or files for a language released after Visual Studio ships into a Solution Folder, and the solution folder would not know how to compile those files. For example, there is no mechanism for using conditional compilation in a solution folder, what would be the correct way to compile a C# file that contains conditionals?

     

    Unless a file is contained within a project that was designed to compile that file type - a .cs file in a C# project, a .cpp file in a C++ project, a .vb file in a VB project - then trying to retrieve a code model is not possible. For solution folders we always return NULL when the CodeModel or FileCodeModel properties are called.

     

    Craig

    Tuesday, September 25, 2007 12:45 AM
  • Thanks for that Craig. Guess I will have to force them to open up the solution then!

     

    Cheers.

    Monday, October 1, 2007 2:15 PM