public bool Build() { if (this.dirty) { this.Flush(); } if (this.executable == null) { SaveFileDialog dlg = new SaveFileDialog(); dlg.DefaultExt = "exe"; dlg.Filter = "Executables|*.exe"; if (dlg.ShowDialog() == DialogResult.OK) { this.executable = dlg.FileName; } } if (this.executable != null) { SampleTypeResolutionService strs = this.host.GetService(typeof(ITypeResolutionService)) as SampleTypeResolutionService; CompilerParameters cp = new CompilerParameters(); foreach (Assembly assm in strs.RefencedAssemblies) { cp.ReferencedAssemblies.Add(assm.Location); foreach (AssemblyName refAssmName in assm.GetReferencedAssemblies()) { Assembly refAssm = Assembly.Load(refAssmName); cp.ReferencedAssemblies.Add(refAssm.Location); } } cp.GenerateExecutable = true; cp.OutputAssembly = this.executable; cp.MainClass = this.host.RootComponent.Site.Name + "Namespace." + this.host.RootComponent.Site.Name; CompilerResults cr = new CSharpCodeProvider().CreateCompiler().CompileAssemblyFromDom(cp, this.codeCompileUnit); if (cr.Errors.HasErrors) { string errors = ""; foreach (CompilerError error in cr.Errors) { errors = errors + error.ErrorText + "\n"; } MessageBox.Show(errors, "Errors during compile."); } return(!cr.Errors.HasErrors); } return(false); }
public override void Flush() { if (this.dirty) { XmlDocument document = new XmlDocument(); document.AppendChild(document.CreateElement("DOCUMENT_ELEMENT")); IComponent root = this.host.RootComponent; Hashtable nametable = new Hashtable(this.host.Container.Components.Count); document.DocumentElement.AppendChild(this.WriteObject(document, nametable, root)); foreach (IComponent comp in this.host.Container.Components) { if (!((comp == root) || nametable.ContainsKey(comp))) { document.DocumentElement.AppendChild(this.WriteObject(document, nametable, comp)); } } CodeCompileUnit code = new CodeCompileUnit(); CodeNamespace ns = new CodeNamespace(root.Site.Name + "Namespace"); ns.Imports.Add(new CodeNamespaceImport("System")); SampleTypeResolutionService strs = this.host.GetService(typeof(ITypeResolutionService)) as SampleTypeResolutionService; foreach (Assembly assm in strs.RefencedAssemblies) { ns.Imports.Add(new CodeNamespaceImport(assm.GetName().Name)); } RootDesignerSerializerAttribute a = TypeDescriptor.GetAttributes(root)[typeof(RootDesignerSerializerAttribute)] as RootDesignerSerializerAttribute; CodeDomSerializer cds = Activator.CreateInstance(this.host.GetType(a.SerializerTypeName)) as CodeDomSerializer; IDesignerSerializationManager manager = this.host.GetService(typeof(IDesignerSerializationManager)) as IDesignerSerializationManager; CodeTypeDeclaration td = cds.Serialize(manager, root) as CodeTypeDeclaration; CodeConstructor con = new CodeConstructor(); con.Attributes = MemberAttributes.Public; con.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "InitializeComponent"), new CodeExpression[0])); td.Members.Add(con); CodeEntryPointMethod main = new CodeEntryPointMethod(); main.Name = "Main"; main.Attributes = MemberAttributes.Public | MemberAttributes.Static; main.CustomAttributes.Add(new CodeAttributeDeclaration("System.STAThreadAttribute")); main.Statements.Add(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Application)), "Run"), new CodeExpression[] { new CodeObjectCreateExpression(new CodeTypeReference(root.Site.Name), new CodeExpression[0]) })); td.Members.Add(main); ns.Types.Add(td); code.Namespaces.Add(ns); this.dirty = false; this.xmlDocument = document; this.codeCompileUnit = code; this.UpdateCodeWindows(); } }