动态生成代码程序

代码段一:
using  System;
using  System.Collections.Generic;
using  System.Reflection;
using  System.Text;
using  System.CodeDom;
using  System.CodeDom.Compiler;

namespace  csrepl
{
    
class Program
    
{
        
static string funcprefix = "using System; "
            
+ "public delegate void Proc(); "
            
+ "public class Wrapper {  "
            
+ "  public static object Set(string name, object value) {  "
            
+ "    AppDomain.CurrentDomain.SetData(name, value); "
            
+ "    return value;  "
            
+ "  } "
            
+ "  public static object Get(string name) {  "
            
+ "    return AppDomain.CurrentDomain.GetData(name); "
            
+ "  } "
            
+ "  public static object Invoke(Proc proc) {  "
            
+ "    proc(); "
            
+ "    return null;  "
            
+ "  } "
            
+ "  public static object Eval() { return  ";
        
static string funcsuffix = ";  } }";

        
static string StringEval(string expr)
        
{
            
string program = funcprefix + expr + funcsuffix;

            ICodeCompiler compiler 
= CodeDomProvider.CreateProvider("C#").CreateCompiler();
            CompilerParameters cp 
= new CompilerParameters();
            cp.GenerateExecutable 
= false;
            cp.GenerateInMemory 
= true;

            CompilerResults results 
= compiler.CompileAssemblyFromSource(cp, program);
            
if (results.Errors.HasErrors)
            
{
                
if (results.Errors[0].ErrorNumber == "CS0029")
                    
return StringEval("Invoke(delegate { " + expr + "; })");
                
return results.Errors[0].ErrorText;
            }

            
else
            
{
                Assembly assm 
= results.CompiledAssembly;
                Type target 
= assm.GetType("Wrapper");
                MethodInfo method 
= target.GetMethod("Eval");
                
object result = method.Invoke(nullnull);
                
return result == null ? null : result.ToString();
            }

        }


        
static void Main(string[] args)
        
{
            
while (true)
            
{
                Console.Write(
"");
                Console.Out.Flush();
                
string expr = Console.ReadLine();
                
if (expr == null)
                    
break;
                
try
                
{
                    
string result = StringEval(expr);
                    Console.WriteLine(result);
                }

                
catch (TargetInvocationException ex)
                
{
                    Console.WriteLine(ex.InnerException.GetType().Name 
+ "" + ex.InnerException.Message);
                }

                
catch (Exception ex)
                
{
                    Console.WriteLine(ex.GetType().Name 
+ "" + ex.Message);
                }

            }


        }

    }

}


代码段2:
 
private   void  Compiler( string  code)
{
    CompilerParameters vCompilerParameters 
= new CompilerParameters();
    vCompilerParameters.GenerateExecutable 
= false;
    vCompilerParameters.GenerateInMemory 
= true;
    vCompilerParameters.ReferencedAssemblies.Add(
"System.Windows.Forms.dll");
    
string vSource =
        
"using System.Windows.Forms; " +
        
"public class Temp " +
        
"{ " +
        
"    public void Test() " +
        
"    { " +
        
"         " + code + " " +
        
"    } " +
        
"} ";
    CompilerResults vCompilerResults 
=
        CodeDomProvider.CreateProvider(
"CSharp").CompileAssemblyFromSource(vCompilerParameters, vSource);

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


示例:
http://file.mofile.com/
提取码:5965304761651400
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值