The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Generating and executing C# from within C#

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.
Jeff Kotula Send private email
Wednesday, January 19, 2005
 
 
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
 
 
google for codedom.  There is a tone of information regarding exactly this.
Aaron F Stanton Send private email
Wednesday, January 19, 2005
 
 
tone?  wtf?

ton.

or tonne.

but not tone.
Aaron F Stanton Send private email
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...
Jeff Kotula Send private email
Wednesday, January 19, 2005
 
 
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...
John Rusk Send private email
Wednesday, January 19, 2005
 
 
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.
Aaron F Stanton Send private email
Wednesday, January 19, 2005
 
 
Have you searched CodeProject? I though I saw something along these lines there...
TownDrunk Send private email
Wednesday, January 19, 2005
 
 
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.
John Rusk Send private email
Thursday, January 20, 2005
 
 
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. :-)
John Rusk Send private email
Thursday, January 20, 2005
 
 
Thanks for all the feedback. Promising avenues...
Jeff Kotula Send private email
Thursday, January 20, 2005
 
 
Another .NET alternative is Reflection Emit, it's lower level than C# and might work well for you if the meat of your code is already in your program and the user actions are just calls to that code with different params.
Glen Low Send private email
Thursday, February 03, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz