在我的代码中,我正在阅读一个文本文件,编译它并尝试运行它, 我有引用的问题(我认为)这是我的代码:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Runtime.InteropServices;
using System.Text;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;
namespace IFCViewer
{
static class Program
{
static public void Main()
{
String code_file = System.IO.File.ReadAllText(@"C:\tmp\my_code.txt");
/* Element DB */
List<DB.IFCArray> Element_list = new List<DB.IFCArray>();
/* Relationships DB */
List<DB.RelObj> Rel_list = new List<DB.RelObj>();
bool Flag = false;
CompileAndRun(code_file);
}
static void CompileAndRun(string code)
{
CompilerParameters CompilerParams = new CompilerParameters();
string outputDirectory = Directory.GetCurrentDirectory();
CompilerParams.GenerateInMemory = true;
CompilerParams.TreatWarningsAsErrors = false;
CompilerParams.GenerateExecutable = false;
CompilerParams.CompilerOptions = "/optimize";
string[] references = { "System.dll","System.Windows.Forms.dll"};
CompilerParams.ReferencedAssemblies.AddRange(references);
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, code);
if (compile.Errors.HasErrors)
{
string text = "Compile error: ";
foreach (CompilerError ce in compile.Errors)
{
text += "rn " + ce.ToString();
}
throw new Exception(text);
}
Module module = compile.CompiledAssembly.GetModules()[0];
Type mt = null;
MethodInfo methInfo = null;
if (module != null)
{
mt = module.GetType("IFCViewer.Program");
}
if (mt != null)
{
methInfo = mt.GetMethod("Run_My_Code");
}
if (methInfo != null)
{
Console.WriteLine(methInfo.Invoke(null, new object[] {}));
}
}
文本文件包含以下代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;
namespace IFCViewer
{
public static class DynaCode
{
static public void Run_My_Code(List<DB.IFCArray> Element_list,List<DB.RelObj> Rel_list,bool Flag)
{
MessageBox.Show("Dynamic Code Running!!");
}
}
}
DB类在具有相同名称空间的不同文件(IFCViewer)上定义。
namespace IFCViewer
{
public static class DB
{
+ public class IFCArray
+ public class RelObj
}
我收到引用错误:错误CS0234:当我尝试编译并运行文件中的代码时,名称空间'IFCViewer'中不存在类型或命名空间名称'DB'(您是否缺少程序集引用?) , 编译和运行方法确实有效,我已经测试过了。
有什么想法吗?