Why FileCodeModel is Nothing (i.e. null) ?

Hi,

I want to modify code model of every new file added to project, but when ProjectItemsEvents.ItemAdded is fired, and I open this new added file, activate its window and call DTE.ActiveDocument.ProjectItem.FileCodeModel, then FileCodeModel is Nothing. Why it is not initialized How can I access FileCodeModel for newly added item from its event

 

Thank you for your answers.




Answer this question

Why FileCodeModel is Nothing (i.e. null) ?

  • Jim D.

    Thanks for reply, I have set up event handler for listening for C#.NET projects (DTE.Events.GetObject("CSharpProjectItemsEvents")) and I listen for ItemAdded, but in final state I would like to support all available project types (i.e. VB.NET, C++.NET and maybe J#.NET).

    I understand that probably ItemAdded is first event, which newly added item goes thru', and after this event, CodeModel is constructed. But then I am unable to fire other event, which will do processing, which I originally needed, when item was added to project. Too  I am unable to catch other events to do this processing. I tried do it during solution before closing event but I am unable to open file and process its in this time. And also during OnBuildBegin event it is too late to modify file 'cause build is in action.

    I found solution but it is not elegant and in some cases may introduces side effects (in case of multiple build event add-ins, etc.). But I think there is no more elegant solution for this problem now. During ItemAdded event I insert #error to this file and I save its ID to global variable. And then I will watch OnBuildDone event. After first build which will fail, I will correct file delete #error and process code model (as I originally intended in ItemAdded event) and then I will successfully rebuild project.

    Am I right, there is no better solution and no other event in which I can handle changing in CodeModel of newly added file

    Because I need to modify code model of Visual Studio autogenerated file which is added via VS.NET wizard. And after adding this autogenerated file, project will be only compiled and immediately deployed so I have no event in which I can do it ... .



  • Geoff Dean

    Implementing a '2' interface is not required. While we highly suggest that a project do so, they are free to not implement these interfaces if the functionality it provides is not necessary for that language.

    Craig



  • BillRP

    New to VS 2005 is an event that all projects fire, Events2.ProjectItemsEvents and Events2.ProjectsEvents, so you do not need to connect to each project type. Also new to VS 2005 is a property, FileCodeModel2.ParseStatus, which will wait until the parser is complete (vsCMParseStatusComplete), or return if an error occured (vsCMParseStatusError).

    Craig



  • FREDYCOREA

    i ran into this issue as well. it seems that the code model doesn't get built right away. the only way i've found around this is to (programmatically) leave the window and come back to it, by actually activating another window and then this one.

    i posted a question about forcing a rebuild of the code model, but that has yet to be answered...



  • canislupus

    Hi Craig Skibo

    I have a question with the filecodemodel, I developed a add-in project, and I want to modify the code of the file which is generated by the add-in, when I use it in the console project and windows form project, it works fine, but when it is used in the website project , an exception threw and the message like: an instance not set to an object, I have checked my code and found out that when I use the property of the filecodemodel like filecodemodel.codeElements, an error occered,so can you tell me why

    thanks



  • Kevin Draper

    Can you describe what project type (C#, J#, VB, VC, Web, etc) you are trying to get the code model for

    Craig



  • ChrisD

    One final question,

    Dim PI As ProjectItem

    ...

    CType(PI.ContainingProject.CodeModel, CodeModel2).Synchronize()

    throws InvalidCastException and ContainingProject.CodeModel2 also throws error, so how can I access CodeModel2

    Thank you.



  • JL3

    If you really need this functionality, you should open a bug on the Product Feedback forum at http://lab.msdn.microsoft.com/productfeedback/default.aspx. You will need to give the programming language that you are trying to retrieve a code model for.

    Craig



  • Why FileCodeModel is Nothing (i.e. null) ?