/// <summary> /// Compiles and generates the code, and returns a tuple of success/failure and the /// generated filename /// </summary> /// <param name="code">The code to compile</param> private static Tuple <Boolean, String> CompileGeneratedCode(String code) { bool succeeded = false; try { succeeded = DynamicCompiler.ComplileCodeBlock(code); return(TupleHelper.New(true, String.Empty)); } catch (Exception ex) { return(TupleHelper.New(false, ex.Message)); } }
/// <summary> /// Saves the current ViewModel as an XML file or generates C# code for the /// current ViewModel /// </summary> /// <param name="filter">The save file filter to use</param> /// <param name="operation">The SaveOrGenerate operation to use</param> private Tuple <Boolean, String> SaveOrGenerateOperation(String filter, SaveOrGenerate operation) { if (!IsValid) { messageBoxService.ShowError("The current ViewModel is InValid\r\nPlease fix it then retry"); return(null); } else { //Ask the user where they want to save the file, and save it try { saveFileService.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory; saveFileService.FileName = String.Empty; saveFileService.Filter = filter; saveFileService.OverwritePrompt = true; bool?result = saveFileService.ShowDialog(null); if (result.HasValue && result.Value) { //Create a Persistence ViewModel based on the current In Memory ViewModel PesistentVM pesistentVM = new PesistentVM(); pesistentVM.VMName = ViewModelName; pesistentVM.VMType = CurrentViewModelType; pesistentVM.VMNamespace = ViewModelNamespace; foreach (SinglePropertyViewModel propVM in propertiesViewModel.PropertyVMs) { pesistentVM.VMProperties.Add(new PesistentVMSingleProperty { PropertyType = propVM.PropertyType, PropName = propVM.PropName, UseDataWrapper = propVM.UseDataWrapper, ParentViewModelName = ViewModelName }); } bool success = false; FileInfo file = new FileInfo(saveFileService.FileName); //decide what file needs saving/generating switch (operation) { case SaveOrGenerate.Save: //save to XML success = ViewModelPersistence.PersistViewModel( saveFileService.FileName, pesistentVM); ShowSaveOrGenMessage(success, file.Name, SaveOrGenerate.Save); return(TupleHelper.New(success, saveFileService.FileName)); case SaveOrGenerate.Generate: //generate code success = ViewModelPersistence.CreateViewModelCode( saveFileService.FileName, pesistentVM); ShowSaveOrGenMessage(success, file.Name, SaveOrGenerate.Generate); return(TupleHelper.New(success, saveFileService.FileName)); } } } catch (Exception ex) { messageBoxService.ShowError( String.Format("An error occurred trying to {0} the ViewModel\r\n{1}", operation.ToString(), ex.Message)); return(null); } return(null); } }