簡體   English   中英

基於表單的Visual Studio項目可以使用單個C#代碼並使用Codedom進行編譯

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

此代碼使我可以創建基於表單的應用程序。

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!";
        }
    }
}
}

我必須以編程方式制作表格及其控件。 我可以將在Visual Studio(作為Windows窗體應用程序)中完成的程序轉換為該應用程序的輸入,從而避免整個編碼嗎?

您將必須了解有關Winforms項目構建方式的更多信息。 請注意,除了從默認設置更改編譯選項之外,IDE還提供了許多難以理解的功能。 資源,設置和UI設計器的代碼生成器隱藏了許多自動知識。

最好的解決方法是研究IDE生成的構建命令。 工具+選項,項目和解決方案,構建和運行。 將“ MSBuild項目生成輸出詳細程度”設置更改為正常。 現在,“輸出”窗口將向您顯示傳遞給各種構建工具的命令行。

足夠好向您展示代碼片段中缺少的內容,您必須指定編譯器的/ target選項 固定:

  parameters.CompilerOptions = "/target:winexe";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM