| ||
|
The "Design of Software" discussion group has been merged with the main
Joel on Software discussion group.
The archives will remain online indefinitely. |
Hi all. I'm looking for pointers for a technique whereby a C# program can generate C# code, compile and link it, and execute it within the context of the original program. Briefly, the use for this is to build a GUI framework that can journal what is happening in it by writing equivalent C# statements into a file that can later be executed to recreate a series of user actions. I know about the external tools that are available, but they don't really fit the need I have. Any pointers appreciated as always.
I recently saw a demo of a program which uses embedded debug statements, sent to a file, which show the execution state of a program as it executes. There was an after-run reader, which would generate an HTML page (with graphics) of what Objects and methods were active, and when, and what they were waiting for. My point is, you probably want a MUCH simpler expression of what people are doing than using a full-bore language like C# to specify it. Now, if there was an off-the-shelf interpreter for C# that supported this, or a built-in functionality for this, MAYBE you'd want to go with that. Even then, I think you can create a simple event simulation language and engine MUCH more easily than shoe-horning what you want to do into C#.
AllanL5 Wednesday, January 19, 2005
Yeah. I've built this type of system before, but the situation was different. There, the UI was done with a scripting language, so all I had to do was store the commands as they were executed into a file and then I could rerun it by executing the file. This was incredibly slick and useful. (The meat of the app was in C++ that was called from the scripting layer.) I think the same basic scheme will work with C#. There is just the extra layer of interpreting into IL before executing...
You don't actually need to use CodeDOM. CodeDOM represents source code as a tree of objects. Instead of that, you can create the source code as text, and compile that on the fly in .NET. That tends to be easier, in my opinion. I'll try to find some sample code, and post it sometime in the next 12 hours or so...
Yes, but codedom has a couple advantages - such as being able to dump to any language that has a provider, you'll never get a compiler error with it, maybe some others. On the other hand, there are some language constructs (even with C#) that you simply cannot create with CodeDOM. There is indeed a trade.
A correction to my earlier post: yes the class that compile source code is technically part of the "codeDOM" set of classes, but unlike the rest of them, it works with text instead of object trees.
Here's the example that I promised: http://agilekiwi.com/on_the_fly.htm I have (rather hastily) ripped it out of the original place I wrote it, and popped it onto my site. So do let me know if you spot any errors that my haste may have caused. Shouldn't be too bad, since the unit tests all pass. :-)
| |
Powered by FogBugz
