理解代码时出现问题

时间:2011-02-06 11:02:03

标签: c#

我无法理解下面给出的代码,所以任何人都可以帮助我理解代码。

private bool Compile(string path)
{
   bool result;

   using (CSharpCodeProvider prov = new CSharpCodeProvider())
   {
      CompilerParameters param = new CompilerParameters();

      string pathtoicon = "";
      if (File.Exists(Application.StartupPath + "\\icon.ico"))
      {
         pathtoicon = Application.StartupPath + "\\icon.ico";
      }

      param.CompilerOptions = "/target:winexe" + " " + "/win32icon:" + "\"" + pathtoicon + "\"";
      param.GenerateExecutable = true;
      param.IncludeDebugInformation = false;
      param.EmbeddedResources.Add(Environment.GetEnvironmentVariable("TEMP")+"\\points.dat");
      param.OutputAssembly = path;
      param.GenerateInMemory = false;

      param.ReferencedAssemblies.Add("System.dll");
      param.ReferencedAssemblies.Add("System.Data.dll");
      param.ReferencedAssemblies.Add("System.Deployment.dll");
      param.ReferencedAssemblies.Add("System.Drawing.dll");
      param.ReferencedAssemblies.Add("System.Windows.Forms.dll");
      param.ReferencedAssemblies.Add("System.Xml.dll");

      param.TreatWarningsAsErrors = false;
      CompilerResults compresults = prov.CompileAssemblyFromSource(param, Properties.Resources.Program);

      result = compresults.Errors.Count == 0;

      File.Delete(Environment.GetEnvironmentVariable("TEMP") + "\\points.dat");
   }

   return result;
}

1 个答案:

答案 0 :(得分:3)

简而言之,上面的代码是使用.NET框架中可用的编译器类编译新的.NET程序集。

以下是一些可能对您有帮助的文档:

CSharpCodeProvider

CompilerParameters