日期:2014-05-17 浏览次数:20922 次
CSharpCodeProvider provider = new CSharpCodeProvider();
StringBuilder sb = new StringBuilder();
sb.Append("using System;");
sb.Append("namespace TestName");
sb.Append("{");
sb.Append(" public class MyClass");
sb.Append(" {");
sb.Append(" public void Hello()");
sb.Append(" {");
sb.Append(" Console.Writeline(\"Hello World!\");");
sb.Append(" }");
sb.Append(" }");
sb.Append("}");
CompilerParameters param = new CompilerParameters();
param.GenerateExecutable = false;
param.GenerateInMemory = true;
CompilerResults result = provider.CompileAssemblyFromSource(param, sb.ToString());
if (result.Errors.HasErrors)
return;
var assembly = result.CompiledAssembly;
var myClass = assembly.CreateInstance("TestName.MyClass");
myClass.GetType().GetMethod("Hello").Invoke(myClass, null);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class A
{
public void foo() { Console.WriteLine("hello"); }
}
class Program
{
static void Main(string[] args)
{
var newa = Expression.New(typeof(A));
var callfoo = Expression.Call(newa, typeof(A).GetMethod("foo"));
var lambda =&n