动态编译c#

public bool CompileScripts(string assemblyName, string path, bool debug) {
92  
93 // find all CSharp files
94 // TODO: Make compilation more generic
95 string[] scriptFiles = Directory.GetFiles(path, "*.cs");
96  
97 // create a code compiler
98 CSharpCodeProvider provider = new CSharpCodeProvider();
99  
100 // configure params for the script compilation
101 CompilerParameters parms = new CompilerParameters(dynamicAssemblyRefs);
102  
103 // this is an in memory assembly
104 // TODO: Consider compiling to file and loading if no changes have occurred on the next load
105 parms.GenerateInMemory = true;
106 parms.GenerateExecutable = false;
107 parms.IncludeDebugInformation = debug;
108  
109 // compile the list of files found in the script directory
110 CompilerResults results = provider.CompileAssemblyFromFile(parms, scriptFiles);
111  
112 // check for errors and log them
113 if(results.Errors.Count != 0) {
114 StringBuilder errorBuilder = new StringBuilder(results.Errors.Count);
115  
116 string errorMsg = "Script Error [File: {0}, Line: {1}]: {2}]" + Environment.NewLine;
117  
118 foreach(CompilerError error in results.Errors) {
119 errorBuilder.AppendFormat(errorMsg, error.FileName, error.Line, error.ErrorText);
120 }
121  
122 Logger.Instance.LogEvent(errorBuilder.ToString(), LoggingLevel.Errors);
123  
124 return false;
125 }
126  
127 AttachAssembly(results.CompiledAssembly);
128  
129 // success
130 return true;
131 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值