Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个刚编译的程序集编译程序集_C#_.net_Codedom - Fatal编程技术网

C# 从另一个刚编译的程序集编译程序集

C# 从另一个刚编译的程序集编译程序集,c#,.net,codedom,C#,.net,Codedom,我需要在内存中编译程序集,这样可以编译另一个程序集。有一个表单只有一个按钮。这是表格的代码 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.D

我需要在内存中编译程序集,这样可以编译另一个程序集。有一个表单只有一个按钮。这是表格的代码

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;
        }
}
当我单击第一个表单中的按钮1时->显示第二个表单。但是当我在第二个表单中单击按钮1时,我有一个FileNotFound异常。我做错了什么?

非常模糊的样本…
为什么尝试使用
assemblyDomain.DoCallBack(assamblykub)调用
assamblykub()
???这种方法适用于跨域调用。

只需回复assemblyDomain.DoCallBack(assamblykub)使用
assamblykub()
并运行它)

)您是对的。。。这真的很有效。但这是测试的例子,我是用C语言做的,因为更多的程序员使用它。我的真实项目在vb中,它不工作(((@Constantin#u B:试着用VB示例代码问你的问题。这里有很多VB程序员,还有更多的人同时知道C#和VB。祝你好运!非常感谢你……我在VB中有相同的例子,也许你知道为什么它在那里不起作用吗@Constantin_B:是的,俄语是我的母语,但本论坛不允许讲俄语。我从不使用VB,但在浏览了你的代码后,我猜想你的VB代码中也有同样的问题。好的,谢谢……新问题->