public void Constructor0_Deny_Unrestricted () { CompilerErrorCollection coll = new CompilerErrorCollection (); Assert.AreEqual (0, coll.Add (ce), "Add"); Assert.AreSame (ce, coll[0], "this[int]"); coll[0] = ce; coll.CopyTo (array, 0); coll.AddRange (array); coll.AddRange (coll); Assert.IsTrue (coll.Contains (ce), "Contains"); Assert.AreEqual (0, coll.IndexOf (ce), "IndexOf"); coll.Insert (0, ce); coll.Remove (ce); ce.IsWarning = false; Assert.IsTrue (coll.HasErrors, "HasErrors"); Assert.IsFalse (coll.HasWarnings, "HasWarnings"); }
private static Assembly CompileInternal(String outputAssembly, IEnumerable<String> references, CodeDomProvider provider, CompilerErrorCollection Errors, Template tmp) { var options = new CompilerParameters(); foreach (var str in references) { options.ReferencedAssemblies.Add(str); } options.WarningLevel = 4; CompilerResults results = null; if (Debug) { #region 调试状态,把生成的类文件和最终dll输出到XTemp目录下 var tempPath = XTrace.TempPath; //if (!String.IsNullOrEmpty(outputAssembly)) tempPath = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(outputAssembly)); if (!String.IsNullOrEmpty(outputAssembly) && !outputAssembly.Equals(".dll")) tempPath = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(outputAssembly)); if (!String.IsNullOrEmpty(tempPath) && !Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); var files = new List<String>(); foreach (var item in tmp.Templates) { if (item.Included) continue; String name = item.Name.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) ? item.Name : item.ClassName; // 猜测后缀 Int32 p = name.LastIndexOf("_"); if (p > 0 && name.Length - p <= 5) name = name.Substring(0, p) + "." + name.Substring(p + 1, name.Length - p - 1); else if (!name.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) name += ".cs"; name = Path.Combine(tempPath, name); File.WriteAllText(name, item.Source, Encoding.UTF8); files.Add(name); } #endregion if (!String.IsNullOrEmpty(outputAssembly) && !outputAssembly.Equals(".dll")) { options.TempFiles = new TempFileCollection(tempPath, false); options.OutputAssembly = Path.Combine(tempPath, outputAssembly); options.GenerateInMemory = true; options.IncludeDebugInformation = true; } results = provider.CompileAssemblyFromFile(options, files.ToArray()); } else { List<String> sources = new List<String>(); foreach (var item in tmp.Templates) { sources.Add(item.Source); } options.GenerateInMemory = true; results = provider.CompileAssemblyFromSource(options, sources.ToArray()); } #region 编译错误处理 if (results.Errors.Count > 0) { Errors.AddRange(results.Errors); var sb = new StringBuilder(); CompilerError err = null; foreach (CompilerError error in results.Errors) { error.ErrorText = error.ErrorText; //if (String.IsNullOrEmpty(error.FileName)) error.FileName = inputFile; if (!error.IsWarning) { String msg = error.ToString(); if (sb.Length < 1) { String code = null; // 屏蔽因为计算错误行而导致的二次错误 try { code = tmp.FindBlockCode(error.FileName, error.Line); } catch { } if (code != null) { msg += Environment.NewLine; msg += code; } err = error; } else sb.AppendLine(); sb.Append(msg); } } if (sb.Length > 0) { TemplateException ex = new TemplateException(sb.ToString()); ex.Error = err; throw ex; } } else { try { options.TempFiles.Delete(); } catch { } } #endregion if (!results.Errors.HasErrors) { try { return results.CompiledAssembly; } catch { } } return null; }