views:

120

answers:

2

I'm writing a desktop application. It would need to generate c# code on client machines.

I thought of using CodeDOM (complex) or simple string manipulations (fragile). What else? Which way is recommended?

+6  A: 

T4? http://msdn.microsoft.com/en-us/library/bb126445.aspx

Bertvan
AFAIK I'm not allowed to redistribute it, is it still the case?
Yaron Naveh
@Yaron, what wouldn't you be allowed to distribute?
Bertvan
T4 can be used to generate classes from a template that in turn generate files are runtime, making this a possible option. See this link for more detail http://msdn.microsoft.com/en-us/library/ee844259.aspx.
Chris Taylor
If you're talking about runtime code generation, you might want to take a look at the ILGenerator class (IL.Emit()) http://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator(VS.95).aspxHowever, I'm not very familiar with that myself, so won't be able to help you any further than the link :)
Bertvan
I need to generate code at customer environment, not at design time. Does T$ fit this scenario, both technically and legaly?
Yaron Naveh
I need to generate C# not IL
Yaron Naveh
T4 is design time, AFAIK
Bertvan
Ah, apparently you could feed the T4 engine at runtime, but then the client/user needs Visual Studio Installed. Although, there seems to be a MONO alternative as well: http://anonsvn.mono-project.com/viewvc/trunk/monodevelop/main/src/addins/TextTemplating/ .
Bertvan
With VS 2010 T4 can be redistributed http://blogs.msdn.com/b/garethj/archive/2008/11/11/dsl-2010-feature-dives-t4-preprocessing-rationale.aspx
Yaron Naveh
A: 

If you mean run dynamic code, then: CS-Script is the most mature solution. With CS-Script you could even roll your own template based, custom code generator, like the scripting used by MyGeneration.

Keith Blows