简体   繁体   中英

How do I pass parameters in run-time compilation in c#, winforms?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM