简体   繁体   English

如何在c#,winforms的运行时编译中传递参数?

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

I'm stuck on run-time compilation and CodeDom. 我坚持运行时编译和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. 我需要将一个参数传递给这个Test表单(一个Panel控件列表)来填充表单。

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. 编辑最后,我放弃CodeDom并使用Mono.Cecil ,注入.exe文件,包含来自我的主程序的信息。

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. 但是,在命令行上传递.Net对象将不起作用。

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 ... 如果你想传递somthing托管,你将不得不使用你的新程序集与一些后期绑定并将你的对象传递给构造函数,而不是取决于你编译的代码接受,如果你在设计时有...

Are you re-writing Visual Studio? 你在重写Visual Studio吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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