Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an existing way to turn source code back into a CodeCompileUnit?

We use the DesignSurface and all that good IDesignerHost goodness in our own designer. The designed forms are then persisted in our own bespoke format and all that works great. WE also want to export the forms to a text-based format (which we've done as it isn't that difficult).

However, we also want to import that text back into a document for the designer which involves getting the designer code back into a CodeCompileUnit. Unfortunately, the Parse method is not implemented (for, no doubt, good reasons). Is there an alternative? We don't want to use anything that wouldn't exist on a standard .NET installation (like .NET libraries installed with Visual Studio).

My current idea is to compile the imported text and then instantiate the form and copy its properties and controls over to the design surface object, and just capture the new CodeCompileUnit, but I was hoping there was a better way. Thanks.


UPDATE: I though some might be interested in our progress. So far, not so good. A brief overview of what I've discovered is that the Parse method was not implemented because it was deemed too difficult, open source parsers exist that do the work but they're not complete and therefore aren't guaranteed to work in all cases (NRefactory is one of those from the SharpDevelop project, I believe), and the copying of controls across from an instance to the designer isn't working as yet. I believe this is because although the controls are getting added to the form instance that the designer surface wraps, the designer surface is not aware of their inclusion. Our next attempt is to mimic cut/paste to see if that solves it. Obviously, this is a huge nasty workaround, but we need it working so we'll take the hit and keep an eye out for alternatives.

like image 923
Jeff Yates Avatar asked Sep 30 '08 23:09

Jeff Yates


1 Answers

You could always write your own C# parser. That way you can be sure of it's completeness.

In your case, because you don't need anything like intellisense, you could probably get away with just using a parser generator.

Even if you wrote one by hand, however, it probably wouldn't take you more than about a month.

like image 182
Scott Wisniewski Avatar answered Sep 28 '22 05:09

Scott Wisniewski