/// <summary> /// 输出生成结果 /// </summary> public void Output(GenResult result) { if (result == null) { // do nothing } else if (result.GenResultType == GenResultTypes.Message) { if (result.Message == null) { return; } using (FOutputText f = new FOutputText(result.Message)) { f.ShowDialog(); } } else if (result.GenResultType == GenResultTypes.CodeSegment) { using (FOutputCode f = new FOutputCode(result.CodeSegment)) { f.ShowDialog(); } } else if (result.GenResultType == GenResultTypes.CodeSegments) { using (FOutputCodes f = new FOutputCodes(result.CodeSegments)) { f.ShowDialog(); } } else if (result.GenResultType == GenResultTypes.File) { CleanOutput(); Output(result.File.Key, result.File.Value); PopupOutput(); } else if (result.GenResultType == GenResultTypes.Files) { CleanOutput(); foreach (KeyValuePair <string, byte[]> file in result.Files) { Output(file.Key, file.Value); } PopupOutput(); } }
/// <summary> /// 初始化代码生成组件集合 /// todo: 检查对像是否具备默认构造函数,是否具备必须的 Property /// </summary> private void InitComponents() { // 载入内部组件 InitComponents(Assembly.GetExecutingAssembly()); // 载入外部组件 exe 所在文件 Components 目录下面的 *.cs // 查找出 *.cs string[] files = null; try { files = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "Components"), "*.cs", SearchOption.AllDirectories); } catch { } if (files == null || files.Length == 0) { return; } // 加载初始化 CompilerParameters options = new CompilerParameters(); string path_this = Assembly.GetExecutingAssembly().Location; string path_smo = typeof(Microsoft.SqlServer.Management.Smo.Server).Assembly.Location; string path_smo_sfc = typeof(Microsoft.SqlServer.Management.Sdk.Sfc.DataProvider).Assembly.Location; string path_smo_connectinfo = typeof(Microsoft.SqlServer.Management.Common.ServerConnection).Assembly.Location; string path_smo_sqlenum = typeof(Microsoft.SqlServer.Management.Smo.UserDefinedFunctionType).Assembly.Location; string path_35 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Reference Assemblies\Microsoft\Framework\v3.5"); string path_35_core = Path.Combine(path_35, "System.Core.dll"); string path_35_dsext = Path.Combine(path_35, "System.Data.DataSetExtensions.dll"); string path_35_xmllinq = Path.Combine(path_35, "System.Xml.Linq.dll"); options.ReferencedAssemblies.AddRange(new string[] { "System.dll" , "System.Data.dll" , "System.Xml.dll" , "System.Windows.Forms.dll" , "Microsoft.VisualBasic.dll" , path_this , path_smo , path_smo_sfc , path_smo_connectinfo , path_smo_sqlenum //, path_35_core //, path_35_dsext //, path_35_xmllinq }); options.CompilerOptions = "/target:library"; options.GenerateInMemory = true; CSharpCodeProvider provider = new CSharpCodeProvider(); // 载入并报错 try { CompilerResults result = provider.CompileAssemblyFromFile(options, files); if (result.Errors != null && result.Errors.Count > 0) { using (FOutputText f = new FOutputText()) { f.Text = "Load components (*.cs) ccurred some error:"; f.Width = 780; f.Height = 550; foreach (CompilerError ce in result.Errors) { f.WriteLine("FileName = {0}", ce.FileName); f.WriteLine("Line = {0}", ce.Line); f.WriteLine("ErrorNumber = {0}", ce.ErrorNumber); f.WriteLine("ErrorText = {0}", ce.ErrorText); f.WriteLine("Column = {0}", ce.Column); f.WriteLine("IsWarning = {0}", ce.IsWarning); f.WriteLine(2); } f.ShowDialog(); Application.Exit(); } } InitComponents(result.CompiledAssembly); _gens.Sort(new Comparison <IGenComponent>((a, b) => { return(string.Compare(string.Concat(a.Properties[GenProperties.Group], a.Properties[GenProperties.Caption]), string.Concat(b.Properties[GenProperties.Group], b.Properties[GenProperties.Caption]))); })); } catch (Exception ex) { MessageBox.Show(ex.Message); } }