<高手请进> 急! 关于C#动态执行代码...

shinaterry 2007-05-08 05:32:38
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;

namespace TestControls
{
public class ExecuteCSharp
{
public static void Eval()
{
//CSharpCodeProvider provider = new CSharpCodeProvider();
//ICodeCompiler compiler = provider.CreateCompiler();
CodeDomProvider compiler = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();

StringBuilder codeBuilder = new StringBuilder();

codeBuilder.AppendLine("using System;");
codeBuilder.AppendLine("using System.Text;");
codeBuilder.AppendLine("using System.Windows.Forms;");
codeBuilder.AppendLine("using System.Collections.Generic;");
codeBuilder.AppendLine();
codeBuilder.AppendLine("namespace TestControls");
codeBuilder.AppendLine("{");
codeBuilder.AppendLine(" public class Test");
codeBuilder.AppendLine(" {");
codeBuilder.AppendLine(" public void Exec()");
codeBuilder.AppendLine(" {");
codeBuilder.AppendLine(" MessageBox.Show(\"测试ing...\");");
codeBuilder.AppendLine(" }");
codeBuilder.AppendLine(" }");
codeBuilder.AppendLine("}");

cp.GenerateExecutable = false;
cp.GenerateInMemory = true;

CompilerResults cr = compiler.CompileAssemblyFromSource(cp, codeBuilder.ToString());

if (!cr.Errors.HasErrors) //注意、注意:此处通过不了
{
Assembly a = cr.CompiledAssembly;
Type t = a.GetType("Test");
MethodInfo mi = t.GetMethod("Exec", BindingFlags.Static | BindingFlags.Public);
mi.Invoke(null, new object[0]);
}
}
}
}



using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TestControls
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
ExecuteCSharp.Eval();
}
}
}


为什么我调用不了[动态代码]里的Test(类) -> Exec方法呢?

动态编译根本通过不了,是不是我用的方法错了,请高手指示...
...全文
747 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinaterry 2007-05-08
  • 打赏
  • 举报
回复
新贴地址:http://community.csdn.net/Expert/topic/5517/5517522.xml?temp=.1310236

请大家继续关注...

谢谢...
shinaterry 2007-05-08
  • 打赏
  • 举报
回复
终于搞定了! ^0^

不过我还有一个疑问:

不知道[动态代码]在运行时编译后,能否实现调用主应用程序里面原来的方法呢?(我尝试ing...)

描述得比较模糊,希望大家能够明白...

如果这个问题能解决的话,我会另外开贴回报的...

谢谢...
shinaterry 2007-05-08
  • 打赏
  • 举报
回复
最近任务很多...

所以晚了下班...

不好意思! 另外要感谢大家热心的支持...

谢谢...
王集鹄 2007-05-08
  • 打赏
  • 举报
回复
// 载入其他库

private void button1_Click(object sender, EventArgs e)
{
ICodeCompiler vCodeCompiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters vCompilerParameters = new CompilerParameters();
vCompilerParameters.GenerateExecutable = false;
vCompilerParameters.GenerateInMemory = true;
vCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); // 关键语句
string vSource =
"using System.Windows.Forms;\n" +
"public class Temp\n" +
"{\n" +
" public void Test()\n" +
" {\n" +
" MessageBox.Show(\"Hello World!\");\n" +
" }\n" +
"}\n";
CompilerResults vCompilerResults =
vCodeCompiler.CompileAssemblyFromSource(vCompilerParameters, vSource);

Assembly vAssembly = vCompilerResults.CompiledAssembly;
object vTemp = vAssembly.CreateInstance("Temp");
MethodInfo vTest = vTemp.GetType().GetMethod("Test");
vTest.Invoke(vTemp, null);
}

hegh736 2007-05-08
  • 打赏
  • 举报
回复
lz还没下班呀!太强了!
qiyu20031022 2007-05-08
  • 打赏
  • 举报
回复
顶,看来问题解决了呀,哈哈
shinaterry 2007-05-08
  • 打赏
  • 举报
回复
今晚下班后结贴...
shinaterry 2007-05-08
  • 打赏
  • 举报
回复
TO:lovefootball

很感谢你热心的帮忙...

现在可以啦!
之前我也在代码里也加过:
String[] referenceAssemblies = { "System.dll", "System.Windows.Forms.dll" };
CompilerParameters cp = new CompilerParameters(referenceAssemblies);
可是报了空引用异常...

原来是[命名空间]和[静态方法]的原因...

不过我想知道!
动态编译代码这种手法是不是不可以有[命名空间]呢?还有里面的方法一定是静态的吗?

如果我必须要在动态类里面加入[命名空间]的话,该怎么办呢?

请指教...
cby1990 2007-05-08
  • 打赏
  • 举报
回复
楼上的正解
lovefootball 2007-05-08
  • 打赏
  • 举报
回复
CodeDomProvider compiler = new CSharpCodeProvider();
String[] referenceAssemblies = { "System.dll", "System.Windows.Forms.dll" };

CompilerParameters cp = new CompilerParameters(referenceAssemblies);

StringBuilder codeBuilder = new StringBuilder();

codeBuilder.AppendLine("using System;");
codeBuilder.AppendLine("using System.Text;");
codeBuilder.AppendLine("using System.Windows.Forms;");
codeBuilder.AppendLine("using System.Collections.Generic;");
codeBuilder.AppendLine(" public class Test");
codeBuilder.AppendLine(" {");
codeBuilder.AppendLine(" public static void Exec()");
codeBuilder.AppendLine(" {");
codeBuilder.AppendLine(" MessageBox.Show(\"testing...\");");
codeBuilder.AppendLine(" }");
codeBuilder.AppendLine(" }");

cp.GenerateExecutable = false;
cp.GenerateInMemory = true;

CompilerResults cr = compiler.CompileAssemblyFromSource(cp, codeBuilder.ToString());

if (!cr.Errors.HasErrors)
{
Assembly a = cr.CompiledAssembly;
Type t = a.GetType("Test");
t.InvokeMember("Exec", BindingFlags.Public |
BindingFlags.Static | BindingFlags.InvokeMethod,
System.Type.DefaultBinder, null, null);
}
shinaterry 2007-05-08
  • 打赏
  • 举报
回复
UP
shinaterry 2007-05-08
  • 打赏
  • 举报
回复
ExecuteCSharp类已导入System命名空间的,代码有点长,请大家耐心请完...

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧