2

I have the following problem, will try to describe it shortly.

In my program there is to be a possibility to compile a winform to an .exe by clicking on a button.

Now I tried to do it with CodeDom, so in my program I have the following line:

CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, text);

where codeProvider is CodeDomProvider and text is the source from where to compile.

Problem is the winform I need to save as an .exe has a class behind it that uses other classes and forms, and, since parameter 'text' is a string parameter, it has to include all those classes, which results in HUGE amount of code, not to mention plenty of mistakes. Here's an example of what I mean.

There must be other ways, question is, what are they? Thanks in advance!

Community
  • 1
  • 1
tube-builder
  • 686
  • 1
  • 13
  • 29

1 Answers1

1

If you always use the same set of classes, it would be worth putting those common classes in a class library, and referring to that from the dynamically compiled code.

If they're not the same in every case, it's hard to see improvements you expect - if that much code has to be compiled, it has to be compiled, and there's that much code which can have mistakes in it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The set of classes is the same, I'll try to follow your advice, thanks! – tube-builder Apr 11 '11 at 07:33
  • @Jon Skeet Sorry to get back to the question, but I'm stuck again. I've created a class library and when I call it from my main project, creating an object, everything is fine. But how do I pass it to the dynamically compiled code using CodeDom? How do I add it as the source from where to compile? Bearing in mind that the constructor should take a parameter, so it looks like this: `CodeToCompile.TForm test = new CodeToCompile.TForm(TestElement.PanelsWithTestElements);` Thanks a lot in advance! – tube-builder Apr 14 '11 at 17:58
  • @tube-builder: Add a reference in the CompilerParameters you pass to CompileAssemblyFromSource. I'm not sure where the constructor comes into your question... – Jon Skeet Apr 14 '11 at 18:00
  • @Jon Skeet I added the reference like this `parameters.ReferencedAssemblies.Add("CodeToCompile.dll");` Maybe I can't explain properly, I'm sorrty. I need to create an .exe(a winform) from my main project, and to this .exe I have to pass a list of Panel controls to populate this form. So I try to achieve it through CodeDom, unsuccessful so far. – tube-builder Apr 14 '11 at 18:21
  • @tube-builder: how would you expect to pass in controls, when you're starting a new process? – Jon Skeet Apr 14 '11 at 18:29
  • @Jon Skeet It's just that it's the first time I come across using runtime compilation. How can I achieve what I need, then? – tube-builder Apr 14 '11 at 18:47
  • @tube-builder: One option is not to create a separate executable, but just a class library and call into that. You might want to look at what I've done for Snippy: http://csharpindepth.com/Files/snippy.zip – Jon Skeet Apr 14 '11 at 18:52
  • @Jon Skeet Thank you, I will study your code and try to finally solve my problem. I really appreciate that you spend your time on this, thanks again. – tube-builder Apr 14 '11 at 19:01