0

I'm stuck on run-time compilation and CodeDom. Here's a simplified example of what I have so far.

public static void Testing()
    {
        CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
        string Output = "Out.exe";

        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();

        parameters.GenerateExecutable = true;
        parameters.OutputAssembly = Output;
        parameters.ReferencedAssemblies.Add("System.dll");
        parameters.ReferencedAssemblies.Add("System.Drawing.Dll");
        parameters.ReferencedAssemblies.Add("System.Windows.Forms.Dll");
        parameters.CompilerOptions = "/t:winexe";

        string[] text = new string[] { @"C:\MyProject\Test.cs", @"C:\MyProject\Test.Designer.cs",
        @"C:\MyProject\Program.cs"};

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

        Process.Start(Output);

    }

It works perfectly alright, and loads the Test form.

But! I need to pass a parameter to this Test form (a list of Panel controls) to populate the form.

How can I do this? Maybe, I am looking in the wrong direction, and it has to be done in a different way? Thanks a lot in advance!

EDIT In the end, I give up on CodeDom and used Mono.Cecil instead, injecting .exe file with information from my main program.

tube-builder
  • 686
  • 1
  • 13
  • 29
  • 2
    Maybe you can pass that parameter as a command-line argument and handle it in the child app? – khachik Apr 14 '11 at 12:27
  • @khachik Thank you for your answer! Could you, please, provide more information on how to pass a parameter to command-line in dynamic compilation? – tube-builder Apr 14 '11 at 12:41
  • Can you please explain your requirement. I am not getting it clear. – Subhash Lama Apr 14 '11 at 12:53
  • @Subhash Lama Shortly, what I need to achieve is the following. The user should have the possibility to first populate a list of Panel controls with various stuff, and then create an .exe file which includes this Test form with Panels on it. So, I need to pass the list of Panels to the class, which is to be compiled on the fly. – tube-builder Apr 14 '11 at 12:59
  • You are *not* close. Create a sample Winforms project first that does this. Only then will you know what code to generate. – Hans Passant Apr 14 '11 at 13:08
  • @Hans Passant I'm sorry, I'm stupid but I don't understand, what you mean by creating a sample winforms project that does this. Does what exactly? Because I have a project that allows the user to populate the panels. I need to provide them with possibility to click on the Finish button and get the .exe with all the info they entered. – tube-builder Apr 14 '11 at 13:19

1 Answers1

2

What you are doing is compiling an executable assembly then starting it in another process.

If you want to pass it information, command line arguments are one option. However, passing a .Net object on the command line will not work.

If you want to pass somthing managed you will have to use your new assembly with some late binding and pass your object to the constructor perhaps, rather depends what the code you are compiling accepts, if you have that at design time ...

Are you re-writing Visual Studio?

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • Thanks for your answer! This all seems a bit too complicated for such a simple thing that I'm trying to achieve. Above I tried to explain what I'm doing in my comment to Subhash Lama. – tube-builder Apr 14 '11 at 13:06