代码:
文件1: class Class2
{
public Class2()
{
Console.WriteLine("class2");
}
}
文件2://用的是动态编译,代码可以不看,只看 红色字体的部分
static void Main(string[] args)
{ CSharpCodeProvider codeProvide = new CSharpCodeProvider();
ICodeCompiler codeCompiler = codeProvide.CreateCompiler();
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.ReferencedAssemblies.Add("System.dll");
compilerParameters.ReferencedAssemblies.Add("System.Core.dll");
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
CompilerResults cr = codeCompiler.CompileAssemblyFromSource(compilerParameters, GenerateCode());
if (cr.Errors.HasErrors)
{
Console.WriteLine("编译错误:");
foreach (CompilerError err in cr.Errors)
{
Console.WriteLine(err.ErrorText);
}
}
else
{
Assembly assembly = cr.CompiledAssembly;
object helloword = assembly.CreateInstance("PINQ_test_ConsoleApplication.HelloWorld");
MethodInfo methodInfo = helloword.GetType().GetMethod("OutPut");
object[] objs = new object[1];
objs[0] = "fuckYou";
char[] cc = (char[])methodInfo.Invoke(helloword, objs);
Console.WriteLine(cc[0].ToString());
Console.WriteLine(new String(cc));
} Console.ReadLine();
}
static string GenerateCode()
{
StringBuilder sb = new StringBuilder();
sb.Append("using System;using System.Linq;");
sb.Append(Environment.NewLine);
sb.Append("namespace PINQ_test_ConsoleApplication");
sb.Append(Environment.NewLine);
sb.Append("{");
sb.Append(Environment.NewLine);
sb.Append(" public class HelloWorld");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" public char[] OutPut(string str)");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append("Class2 cc=new Class2(); return str.ToArray();");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append("}"); string code = sb.ToString();
//Console.WriteLine(code);
//Console.WriteLine(); return code;
}这个时候显示的是错误的,因为这是两个文件,虽然我定义了同一个命名空间,但是,系统好像并不把他们当做一个命名空间处理,请问,应该如何解决?
文件1: class Class2
{
public Class2()
{
Console.WriteLine("class2");
}
}
文件2://用的是动态编译,代码可以不看,只看 红色字体的部分
static void Main(string[] args)
{ CSharpCodeProvider codeProvide = new CSharpCodeProvider();
ICodeCompiler codeCompiler = codeProvide.CreateCompiler();
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.ReferencedAssemblies.Add("System.dll");
compilerParameters.ReferencedAssemblies.Add("System.Core.dll");
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = true;
CompilerResults cr = codeCompiler.CompileAssemblyFromSource(compilerParameters, GenerateCode());
if (cr.Errors.HasErrors)
{
Console.WriteLine("编译错误:");
foreach (CompilerError err in cr.Errors)
{
Console.WriteLine(err.ErrorText);
}
}
else
{
Assembly assembly = cr.CompiledAssembly;
object helloword = assembly.CreateInstance("PINQ_test_ConsoleApplication.HelloWorld");
MethodInfo methodInfo = helloword.GetType().GetMethod("OutPut");
object[] objs = new object[1];
objs[0] = "fuckYou";
char[] cc = (char[])methodInfo.Invoke(helloword, objs);
Console.WriteLine(cc[0].ToString());
Console.WriteLine(new String(cc));
} Console.ReadLine();
}
static string GenerateCode()
{
StringBuilder sb = new StringBuilder();
sb.Append("using System;using System.Linq;");
sb.Append(Environment.NewLine);
sb.Append("namespace PINQ_test_ConsoleApplication");
sb.Append(Environment.NewLine);
sb.Append("{");
sb.Append(Environment.NewLine);
sb.Append(" public class HelloWorld");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" public char[] OutPut(string str)");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append("Class2 cc=new Class2(); return str.ToArray();");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append("}"); string code = sb.ToString();
//Console.WriteLine(code);
//Console.WriteLine(); return code;
}这个时候显示的是错误的,因为这是两个文件,虽然我定义了同一个命名空间,但是,系统好像并不把他们当做一个命名空间处理,请问,应该如何解决?
解决方案 »
- C#如何显示某一个图片上的指定区域?
- 分布式数据库系统框架
- c#多线程采集,出现自动关闭软件
- 想用MODI.Document对象直接识别网上的PNG格式的图片(求教高手)
- C#.NET 2005中可以操作UNIX系统的命令吗?
- 大家好,如果从上海请一位精通C/S 及 B/S等分布式企业级计算程序的分析及架构师或者相当有经验的人员,年龄在35岁以上,年薪大约需要多少?!
- DataGrid当处于编辑状态的时候,如何不显示删除按钮列?
- C#的析构函数的默认访问级别是什么? 可否编程获得一个函数的访问级别?
- alert()为么会失效
- SQL语句日期过滤问题。急!急!
- 菜鸟求助,后台值前台取不出来!!!
- 绑定后的刷新问题
这里错误