I am writing a wizard that adds validation and other code to an existing ASP.NET web page in an automated fashion. I can find the code-behind file and get its FileCodeModel, find the elements within etc. That all works well, at least once I found my way around the MANY functions that cause a Not_Implemented_Exception (!) (for instance, the Children property and CreateUniqueID).
But as has been discussed elsewhere here, it doesn't go any lower than CodeFunction. I.e. the internals of the function are not parsed. When I need to insert code into the function itself, am I left with directly mucking around in the text buffer?
I've seen some references on the net to a VsCodeDomProvider inside Visual Studio, that actually does implement ICodeParser. (Unlike the CodeDom implementations that come with .NET, which apparently return "null" when you try to get a pointer to a parser). I was wondering if it is worth the trouble of working with this to let it parse the code deeper than the FileCodeModel does. The statements I need to generate are pretty simple, like "if ( !IsValid) CallSomeFunction()" and such like that.
It seemed like the advantage of using the CodeDom this way was my code generator would be reasonably language-independent. If I have to muck around in the text buffer, I'm going to have to keep at least 2 copies of each code segment I want to insert (one for C# and one for VB.NET).
The wall I ran into is how to obtain the VsCodeDomProvider from inside an AddIn. I've seen sample code on the net that shows how to do it from a CodeGenerator. (There's even a BaseCodeGenerator project with source code provided by Microsoft). But that seems to rely on calling GetService, and I can't find an object in my context that lets me do that. The Window.Object and HTMLWindow.CurrentTabObject don't seem to want to cast to a IDesignerHost when I'm editing the web page. And when I open the code-behind file, they don't either. I poked around in various objects, and tried casting to various other children of IServiceProvider, but I could never get my hands on an object that implemented GetService so I could call it to get the VsCodeDomProvider.
Am I wasting my time on a dead end? Is the correct way to do this just to get down to the CodeFunction and muck around in the text buffer?
Thanks in advance for the help.
--Tim Farley
Tim Farley - 02 Aug 2004 22:17 GMT
To clarify a bit: when I say Wizard, I don't mean a Wizard as Visual Studio defines them (like a New Project Wizard) but a generic wizard in the Windows sense (a multi-step dialog). The user invokes it from a menu item or a toolbar, and it steps them through fixing problems with their web form.
Other than this FileCodeModel problem, I've got the rest of the code working.
--Tim Farley