2011-04-20 57 views
1

对不起,我的英语,我不太好... 我需要在内存中编译程序集,可以编译另一个。有一个按钮的表单。这里是形式从另一个组件compcompill组件只是compilled组件

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

    namespace testC 
    { 
     public partial class Form1 : Form 
     {static string str_tb; 
      public Form1() 
      { 
       InitializeComponent(); 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       openkubik(); 
      } 
      private void openkubik() 
      { 
       Thread th = new Thread(new ThreadStart(sborkakub)); 
       th.Start(); 
      } 

      private void sborkakub() 
      { 
       System.Security.Policy.Evidence evi = AppDomain.CurrentDomain.Evidence; 
       AppDomain assemblyDomain; 
       AppDomainSetup assemblyDomainSetup = new AppDomainSetup(); 
       assemblyDomainSetup.ApplicationBase = System.Environment.CurrentDirectory; 
       assemblyDomainSetup.DisallowBindingRedirects = false; 
       assemblyDomainSetup.DisallowCodeDownload = true; 
       assemblyDomainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 
       assemblyDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost; 
       assemblyDomain = AppDomain.CreateDomain("AssemblyDomain", evi, assemblyDomainSetup); 
       assemblyDomain.DoCallBack(assamblykub); 
      } 

      private static void assamblykub() { 
      createtext(); 
      System.CodeDom.Compiler.CodeDomProvider objCodeCompiler = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); 
      System.CodeDom.Compiler.CompilerParameters objCompilerParameters = new System.CodeDom.Compiler.CompilerParameters(); 
       foreach (System.Reflection.Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { 
       objCompilerParameters.ReferencedAssemblies.Add(asm.Location); 
      } 
      objCompilerParameters.CompilerOptions = "/target:winexe"; 
      objCompilerParameters.GenerateExecutable = true; 
      objCompilerParameters.GenerateInMemory = true; 
      objCompilerParameters.IncludeDebugInformation = false; 
      System.CodeDom.Compiler.CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, str_tb); 
      if (objCompileResults.Errors.HasErrors) { 
       MessageBox.Show(String.Format("Error: Line>{0}, {1} # {2}", objCompileResults.Errors[0].Line, objCompileResults.Errors[0].ErrorText, objCompileResults.Errors[0].ErrorNumber)); 
       return; 
      } 
      System.Reflection.Assembly objAssembly = objCompileResults.CompiledAssembly; 
      object objTheClass = objAssembly.CreateInstance("MainClass"); 
      if ((objTheClass == null)) { 
       MessageBox.Show("Can\'t load class..."); 
       return; 
      } 
      try { 
       objTheClass.GetType().InvokeMember("Main", System.Reflection.BindingFlags.InvokeMethod, null, objTheClass, null); 
      } 
      catch (Exception ex) { 
       MessageBox.Show(("Error:" + ex.Message)); 
      } 
     } 

      private static void createtext() 
      { 
       FileStream tempfile = new FileStream((Application.StartupPath + "/temp_open.txt"), FileMode.Open, FileAccess.Read); 
       StreamReader tempfilesr = new StreamReader(tempfile, System.Text.Encoding.GetEncoding(1251)); 
       str_tb = tempfilesr.ReadToEnd(); 
       tempfilesr.Close(); 
       tempfile.Close(); 
       tempfile = null; 
       tempfilesr = null; 
      } 
     } 
    } 

的代码和“temp_open.txt”文件的代码

using System; 
using System.IO; 
using System.CodeDom; 
using System.CodeDom.Compiler; 
using System.Threading; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 


     public partial class MainClass : MarshalByRefObject 
    { 
public static void Main() 
{ 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
} 
} 
    public class Form1 : Form 
    {static string str_tb; 
     private System.ComponentModel.IContainer components = null; 

     private System.Windows.Forms.Button button1; 

      protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 
     private void InitializeComponent() 
     { 
      this.button1 = new System.Windows.Forms.Button(); 
      this.SuspendLayout(); 
      this.button1.Location = new System.Drawing.Point(45, 44); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(313, 52); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "button1"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(425, 143); 
      this.Controls.Add(this.button1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 
} 
     public Form1() 
     {  
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      openkubik(); 
     } 
     private void openkubik() 
     { 
      Thread th = new Thread(new ThreadStart(sborkakub)); 
      th.Start(); 
     } 

     private void sborkakub() 
     { 
      System.Security.Policy.Evidence evi = AppDomain.CurrentDomain.Evidence; 
      AppDomain assemblyDomain; 
      AppDomainSetup assemblyDomainSetup = new AppDomainSetup(); 
      assemblyDomainSetup.ApplicationBase = System.Environment.CurrentDirectory; 
      assemblyDomainSetup.DisallowBindingRedirects = false; 
      assemblyDomainSetup.DisallowCodeDownload = true; 
      assemblyDomainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 
      assemblyDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost; 
      assemblyDomain = AppDomain.CreateDomain("AssemblyDomain", evi, assemblyDomainSetup); 
      assemblyDomain.DoCallBack(assamblykub); 
     } 

     private static void assamblykub() { 
     createtext(); 
     System.CodeDom.Compiler.CodeDomProvider objCodeCompiler = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); 
     System.CodeDom.Compiler.CompilerParameters objCompilerParameters = new System.CodeDom.Compiler.CompilerParameters(); 
      foreach (System.Reflection.Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { 
      objCompilerParameters.ReferencedAssemblies.Add(asm.Location); 
     } 
     objCompilerParameters.CompilerOptions = "/target:winexe"; 
     objCompilerParameters.GenerateExecutable = true; 
     objCompilerParameters.GenerateInMemory = true; 
     objCompilerParameters.IncludeDebugInformation = false; 
     System.CodeDom.Compiler.CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, str_tb); 
     if (objCompileResults.Errors.HasErrors) { 
      MessageBox.Show(String.Format("Error: Line>{0}, {1} # {2}", objCompileResults.Errors[0].Line, objCompileResults.Errors[0].ErrorText, objCompileResults.Errors[0].ErrorNumber)); 
      return; 
     } 
     System.Reflection.Assembly objAssembly = objCompileResults.CompiledAssembly; 
     object objTheClass = objAssembly.CreateInstance("MainClass"); 
     if ((objTheClass == null)) { 
      MessageBox.Show("Can\'t load class..."); 
      return; 
     } 
     try { 
      objTheClass.GetType().InvokeMember("Main", System.Reflection.BindingFlags.InvokeMethod, null, objTheClass, null); 
     } 
     catch (Exception ex) { 
      MessageBox.Show(("Error:" + ex.Message)); 
     } 
    } 

     private static void createtext() 
     { 
      FileStream tempfile = new FileStream((Application.StartupPath + "/temp_open.txt"), FileMode.Open, FileAccess.Read); 
      StreamReader tempfilesr = new StreamReader(tempfile, System.Text.Encoding.GetEncoding(1251)); 
      str_tb = tempfilesr.ReadToEnd(); 
      tempfilesr.Close(); 
      tempfile.Close(); 
      tempfile = null; 
      tempfilesr = null; 
     } 

} 

,当我在第一种形式单击Button - >出现第二种形式。但是当我在第二个窗体中单击按钮1时,我有一个FileNotFound异常。我究竟做错了什么?

回答

1

非常模糊的样本...
为什么你试图拨打assamblykub()assemblyDomain.DoCallBack(assamblykub); ???这种跨域调用的方法。
只是replcae assemblyDomain.DoCallBack(assamblykub);assamblykub()和你会得到它运行)

+0

)))你是正确的......它真的很好地工作。但是这是测试的例子,我在C#中做过,因为更多的程序员使用它。我真正的项目在VB和它是行不通的((( – 2011-04-21 07:15:58

+0

@Constantin_B:尝试用VB示例代码问你的问题。这里有很多VB程序员,并且有更多的人知道C#和VB。祝你好运! – 2011-04-21 07:24:13

+0

非常感谢...我在vb http://www.sql.ru/forum/actualutils.aspx?action=gotomsg&tid=834160&msg=10524896中也有同样的例子,也许你知道它为什么不在那里工作?можетВы говоритепо-русскиАнтонСеменов?))) – 2011-04-21 07:25:41

相关问题