简体   繁体   English

基于表单的Visual Studio项目可以使用单个C#代码并使用Codedom进行编译

[英]Form based visual studio project to single c# code and compile using codedom

This code allows me to make a form based application. 此代码使我可以创建基于表单的应用程序。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using System.Diagnostics;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.button1.Click += new System.EventHandler(this.button1_Click);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
        string Output = "Out.exe";
        Button ButtonObject = (Button)sender;

        textBox2.Text = "";
        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
        parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        parameters.ReferencedAssemblies.Add("System.dll");
        parameters.ReferencedAssemblies.Add("System.Data.dll");
        parameters.ReferencedAssemblies.Add("System.Linq.dll");
        parameters.GenerateExecutable = true;
        parameters.OutputAssembly = Output;
        CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);

        if (results.Errors.Count > 0)
        {
            textBox2.ForeColor = Color.Red;
            foreach (CompilerError CompErr in results.Errors)
            {
                textBox2.Text = textBox2.Text +
                            "Line number " + CompErr.Line +
                            ", Error Number: " + CompErr.ErrorNumber +
                            ", '" + CompErr.ErrorText + ";" +
                            Environment.NewLine + Environment.NewLine;
            }
        }
        else
        {
            textBox2.ForeColor = Color.Blue;
            textBox2.Text = "Success!";
        }
    }
}
}

I have to make the form and its controls programmatically. 我必须以编程方式制作表格及其控件。 Can I convert the the program done in visual studio ( as a windows form application) to the input to this application thus avoiding whole coding? 我可以将在Visual Studio(作为Windows窗体应用程序)中完成的程序转换为该应用程序的输入,从而避免整个编码吗?

You'll have to learn more about the way a Winforms project is built. 您将必须了解有关Winforms项目构建方式的更多信息。 Do beware that the IDE provides lots of hard-to-see glue beyond changing the compile options from their default setting. 请注意,除了从默认设置更改编译选项之外,IDE还提供了许多难以理解的功能。 The code generators for resources, settings and the UI designer hide a lot of automagic. 资源,设置和UI设计器的代码生成器隐藏了许多自动知识。

Best way to go about it is studying the build commands that the IDE generates. 最好的解决方法是研究IDE生成的构建命令。 Tools + Options, Projects and Solutions, Build and Run. 工具+选项,项目和解决方案,构建和运行。 Change the "MSBuild project build output verbosity" setting to Normal. 将“ MSBuild项目生成输出详细程度”设置更改为正常。 The Output window now shows you the command lines passed to the various build tools. 现在,“输出”窗口将向您显示传递给各种构建工具的命令行。

Good enough to show you what's missing from your snippet, you'll have to specify the compiler's /target option . 足够好向您展示代码片段中缺少的内容,您必须指定编译器的/ target选项 Fix: 固定:

  parameters.CompilerOptions = "/target:winexe";

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

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