【问题标题】:CodeDOM Not compilingCodeDOM 未编译
【发布时间】:2013-01-05 05:32:54
【问题描述】:

我想为我的应用程序构建一个构建器,这是构建器的来源

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 Microsoft.CSharp;

namespace MyAppBuilder
{

    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void BuildBtn_Click(object sender, EventArgs e)
    {
        CompilerParameters Params = new CompilerParameters();
        Params.GenerateExecutable = true;

        Params.ReferencedAssemblies.Add("System.dll");
        Params.ReferencedAssemblies.Add("System.Drawing.dll");
        Params.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        Params.ReferencedAssemblies.Add("System.Runtime.InteropServices.dll");
      //  Params.ReferencedAssemblies.Add("System.Diagnostics");
        Params.ReferencedAssemblies.Add("System.Threading.dll");
        Params.ReferencedAssemblies.Add("System.IO.dll");
       //Params.ReferencedAssemblies.Add("Microsoft.Win32.dll");
        Params.OutputAssembly = "output.exe";
        Params.CompilerOptions = " /target:winexe";
        string Source = Properties.Resources.LockerSource;
        CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(Params, Source);
        if (results.Errors.Count < 0)
        {
            MessageBox.Show("nice");
        }
        else
        {
            foreach (var error in results.Errors)
            {
                MessageBox.Show(error.ToString());
            }
        }
    }
}

}

这是我要构建的应用程序的来源 不要关注添加到 CsharpCodeProvider 中的引用程序集

using System;
using System.Drawing;
using System.Windows.Forms;



namespace MyApp
{
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                #region AddControls
                //Basic Form Seetings
                this.Text = "AppName";
                this.ControlBox = false;
                this.ShowInTaskbar = false;
                //Add Contidions TextBox
                RichTextBox conditions = new RichTextBox();
                this.Controls.Add(conditions);
                conditions.Width = this.Width;
                conditions.Height = this.Height / 2;
                conditions.BackColor = this.BackColor;
                conditions.BorderStyle = BorderStyle.None;
                conditions.Text =@"Sometext";
                conditions.Font = new Font(conditions.Font.FontFamily, 8, conditions.Font.Style | FontStyle.Bold);
                conditions.Location = new Point(0, 130);
                conditions.Enabled = false;
                this.Width += 15;
                //Add Contidions TextBox
                //Add CodeTextBox
                TextBox codeBox = new TextBox();
                this.Controls.Add(codeBox);
                codeBox.Width = this.Width - 60;
                codeBox.Location = new Point(20, 10);
                codeBox.TextAlign = HorizontalAlignment.Center;
                //Add CodeTextBox
                //Add DownloadButton
                Button DownloadBtn = new Button();
                this.Controls.Add(DownloadBtn);
                DownloadBtn.Location = new Point(19, 35);
                DownloadBtn.Width = 130;
                DownloadBtn.Height = 30;
                DownloadBtn.Text = "Download Code";
                Button SubmitBtn = new Button();
                this.Controls.Add(SubmitBtn);
                SubmitBtn.Location = new Point(this.Width - 169, 35);
                SubmitBtn.Width = 130;
                SubmitBtn.Height = 30;
                SubmitBtn.Text = "Submit";
                Button VerifyEmailBtn = new Button();
                this.Controls.Add(VerifyEmailBtn);
                VerifyEmailBtn.Location = new Point(19, 70);
                VerifyEmailBtn.Width = codeBox.Width + 1;
                VerifyEmailBtn.Height = 30;
                VerifyEmailBtn.Text = "Click here if you need to verify your email";
                //Add DownloadButton
                #endregion
            }


        }
    }

但我收到以下错误

找不到类型或命名空间名称“Form”(您是否缺少 使用指令或程序集引用)

我理解错误只是无法理解我可能缺少什么。

我希望我能得到一些回应,因为我真的被困住了。

【问题讨论】:

  • 您正在编译的代码似乎没有 InitializeComponent();你可以试着把它拉出来。
  • 如果我删除 initializeComponent() 我会得到一大堆其他错误
  • 那么我还需要添加什么其他代码,是的,可能是菜鸟错误,请指出:)
  • @FaTaL_ErRoR: 抱歉,误读了问题,你的错误不像菜鸟;p

标签: c# codedom


【解决方案1】:

您需要为应用添加合适的入口点。

查看 VS.NET 作为模型生成的 Program.cs 中的 Main() 函数。

您还需要删除 InitializeComponent() 调用。

在这些更改之后,它对我来说编译得很好。

这是我的代码(出于我的目的,我把你的程序变成了一个字符串):

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace formcomp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CompilerParameters Params = new CompilerParameters();
            Params.GenerateExecutable = true;

            Params.ReferencedAssemblies.Add("System.dll");
            Params.ReferencedAssemblies.Add("System.Drawing.dll");
            Params.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            Params.ReferencedAssemblies.Add("System.Runtime.InteropServices.dll");
            Params.ReferencedAssemblies.Add("System.Threading.dll");
            Params.ReferencedAssemblies.Add("System.IO.dll");
            Params.OutputAssembly = "output.exe";
            Params.CompilerOptions = " /target:winexe";
            string Source = GetSource();
            CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(Params, Source);
            if (results.Errors.Count < 0)
            {
                MessageBox.Show("nice");
            }
            else
            {
                foreach (var error in results.Errors)
                {
                    MessageBox.Show(error.ToString());
                }
            }
        }

        private string GetSource()
        {
            string code = "using System; " + Environment.NewLine;
            code += "using System.Drawing;" + Environment.NewLine;
            code += "using System.Windows.Forms;" + Environment.NewLine;
            code += "namespace MyApp" + Environment.NewLine;
            code += "{" + Environment.NewLine;
            code += "public class Program" + Environment.NewLine;
            code += "{" + Environment.NewLine;
            code += "[STAThread]" + Environment.NewLine;
            code += "static void Main()" + Environment.NewLine;
            code += "{" + Environment.NewLine;
            code += "Application.EnableVisualStyles();" + Environment.NewLine;
            code += "Application.SetCompatibleTextRenderingDefault(false);" + Environment.NewLine;
            code += "Application.Run(new Form1());" + Environment.NewLine;
            code += "}" + Environment.NewLine;
            code += "}" + Environment.NewLine;
            code += "        public partial class Form1 : Form" + Environment.NewLine;
            code += "        {" + Environment.NewLine;
            code += "            public Form1()" + Environment.NewLine;
            code += "            {" + Environment.NewLine;
            code += "                #region AddControls" + Environment.NewLine;
            code += "                //Basic Form Seetings" + Environment.NewLine;
            code += "                this.Text = \"AppName\";" + Environment.NewLine;
            code += "                this.ControlBox = false;" + Environment.NewLine;
            code += "                this.ShowInTaskbar = false;" + Environment.NewLine;
            code += "                //Add Contidions TextBox" + Environment.NewLine;
            code += "                RichTextBox conditions = new RichTextBox();" + Environment.NewLine;
            code += "                this.Controls.Add(conditions);" + Environment.NewLine;
            code += "                conditions.Width = this.Width;" + Environment.NewLine;
            code += "                conditions.Height = this.Height / 2;" + Environment.NewLine;
            code += "                conditions.BackColor = this.BackColor;" + Environment.NewLine;
            code += "                conditions.BorderStyle = BorderStyle.None;" + Environment.NewLine;
            code += "                conditions.Text =\"Sometext\";" + Environment.NewLine;
            code += "                conditions.Font = new Font(conditions.Font.FontFamily, 8, conditions.Font.Style | FontStyle.Bold);" + Environment.NewLine;
            code += "                conditions.Location = new Point(0, 130);" + Environment.NewLine;
            code += "                conditions.Enabled = false;" + Environment.NewLine;
            code += "                this.Width += 15;" + Environment.NewLine;
            code += "                //Add Contidions TextBox" + Environment.NewLine;
            code += "                //Add CodeTextBox" + Environment.NewLine;
            code += "                TextBox codeBox = new TextBox();" + Environment.NewLine;
            code += "                this.Controls.Add(codeBox);" + Environment.NewLine;
            code += "                codeBox.Width = this.Width - 60;" + Environment.NewLine;
            code += "                codeBox.Location = new Point(20, 10);" + Environment.NewLine;
            code += "                codeBox.TextAlign = HorizontalAlignment.Center;" + Environment.NewLine;
            code += "                //Add CodeTextBox" + Environment.NewLine;
            code += "                //Add DownloadButton" + Environment.NewLine;
            code += "                Button DownloadBtn = new Button();" + Environment.NewLine;
            code += "                this.Controls.Add(DownloadBtn);" + Environment.NewLine;
            code += "                DownloadBtn.Location = new Point(19, 35);" + Environment.NewLine;
            code += "                DownloadBtn.Width = 130;" + Environment.NewLine;
            code += "                DownloadBtn.Height = 30;" + Environment.NewLine;
            code += "                DownloadBtn.Text = \"Download Code\";" + Environment.NewLine;
            code += "                Button SubmitBtn = new Button();" + Environment.NewLine;
            code += "                this.Controls.Add(SubmitBtn);" + Environment.NewLine;
            code += "                SubmitBtn.Location = new Point(this.Width - 169, 35);" + Environment.NewLine;
            code += "                SubmitBtn.Width = 130;" + Environment.NewLine;
            code += "                SubmitBtn.Height = 30;" + Environment.NewLine;
            code += "                SubmitBtn.Text = \"Submit\";" + Environment.NewLine;
            code += "                Button VerifyEmailBtn = new Button();" + Environment.NewLine;
            code += "                this.Controls.Add(VerifyEmailBtn);" + Environment.NewLine;
            code += "                VerifyEmailBtn.Location = new Point(19, 70);" + Environment.NewLine;
            code += "                VerifyEmailBtn.Width = codeBox.Width + 1;" + Environment.NewLine;
            code += "                VerifyEmailBtn.Height = 30;" + Environment.NewLine;
            code += "                VerifyEmailBtn.Text = \"Click here if you need to verify your email\";" + Environment.NewLine;
            code += "                //Add DownloadButton" + Environment.NewLine;
            code += "                #endregion" + Environment.NewLine;
            code += "            }" + Environment.NewLine;
            code += "        }" + Environment.NewLine;
            code += "    }" + Environment.NewLine;
            return code;
        }
    }
}

【讨论】:

  • 为什么这被否决了?我刚刚使用此代码编译并运行了表单(添加了 AppDomain.CreateDomain("Test").ExecuteAssembly("output.exe"); 这怎么不能回答发帖者的问题?
  • 我可以问你另一个简单的问题 - 我如何将 Microsoft.Win32 && System.Diagnostics.dll 添加到引用的程序集,因为我得到元数据错误文件找不到。谢谢
  • 它们不是 DLL,它们是命名空间。这两个命名空间大多位于 System.dll 和 mscorlib.dll 中。如果您缺少所需的类,请尝试添加 mscrolib.dll。如果您查看给定类的 SDK 文档,它会说明哪个程序集有它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多