From: Daniel C. \(kzu\) <dca...@us...> - 2004-10-10 15:32:03
|
Update of /cvsroot/mvp-xml/Design/v1/src/CustomTools/SGen In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11033/v1/src/CustomTools/SGen Added Files: SGenTool.cs Log Message: Custom tool class that exposes the SGen feature. Uses the XmlSerializerGenerator under the covers. --- NEW FILE: SGenTool.cs --- #region using using System; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Xml; using System.Xml.Schema; using EnvDTE; using VSLangProj; #endregion namespace Mvp.Xml.Design.CustomTools.SGen { /// <summary> /// Generates custom typed XmlSerializers. /// </summary> /// <remarks> /// On any class set the Custom Tool property to "SGen". /// This tool supports C# projects only, as that's the code generated /// by the XmlSerializer class. /// </remarks> [Guid("4F194ABB-9123-4468-ABFC-474899C3A94E")] [CustomTool("SGen", "MVP XML XmlSerializer Generation Tool", false)] [ComVisible(true)] [VersionSupport("7.1")] [CategorySupport(CategorySupportAttribute.CSharpCategory)] public class SGenTool : CustomTool { static string ConfigFile; #region Static config initialization for assembly location resolving static SGenTool() { AssemblyName name = Assembly.GetExecutingAssembly().GetName(); ConfigFile = Path.GetTempFileName() + ".config"; using (StreamWriter sw = new StreamWriter(ConfigFile)) { // Keep serialization files. Required for SGen to work. sw.Write(@"<?xml version='1.0' encoding='utf-8' ?> <configuration> <system.diagnostics> <switches> <add name='XmlSerialization.Compilation' value='4'/> </switches> </system.diagnostics> </configuration>"); } } private static string GetHexString(byte[] token) { StringBuilder sb = new StringBuilder(token.Length * 2); for(int i = 0; i < token.Length; i++) { sb.AppendFormat("{0:X2}", token[i]); } return sb.ToString(); } #endregion Static config initialization for dynamic domains #region GenerateCode /// <summary> /// Generates the output. /// </summary> protected override byte[] GenerateCode(string inputFileName, string inputFileContent) { AppDomain domain = null; try { // Force compilation of the current project. We need the type in the output. ProjectItem item = base.CurrentItem; item.DTE.Solution.SolutionBuild.BuildProject( item.DTE.Solution.SolutionBuild.ActiveConfiguration.Name, item.ContainingProject.UniqueName, true); // Copy Design assembly to output for the isolated AppDomain. string output = item.ContainingProject.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString(); output = Path.Combine(item.ContainingProject.Properties.Item("FullPath").Value.ToString(), output); string asmfile = ReflectionHelper.GetAssemblyPath(Assembly.GetExecutingAssembly()); File.Copy(asmfile, Path.Combine(output, Path.GetFileName(asmfile)), true); CodeClass targetclass = null; foreach (CodeElement element in item.FileCodeModel.CodeElements) { if (element is CodeNamespace) { foreach (CodeElement codec in ((CodeNamespace)element).Members) { if (codec is CodeClass) { targetclass = (CodeClass) codec; break; } } } else if (element is CodeClass) { targetclass = (CodeClass) element; } if (targetclass != null) { break; } } string codefile = Path.GetTempFileName(); AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationName = "Mvp.Xml Design Domain"; setup.ApplicationBase = output; setup.ConfigurationFile = ConfigFile; domain = AppDomain.CreateDomain("Mvp.Xml Design Domain", null, setup); // Runner ctor will dump the output to the file we pass. domain.CreateInstance( Assembly.GetExecutingAssembly().FullName, typeof(SGenRunner).FullName, false, 0, null, new object[] { codefile, targetclass.FullName + ", " + item.ContainingProject.Properties.Item("AssemblyName").Value.ToString(), base.FileNameSpace }, null, null, null); string code; using (StreamReader reader = new StreamReader(codefile)) { code = reader.ReadToEnd(); } return System.Text.Encoding.ASCII.GetBytes(code); } catch (Exception e) { return System.Text.Encoding.ASCII.GetBytes(e.ToString()); } finally { if (domain != null) { AppDomain.Unload(domain); } } } #endregion GenerateCode #region GetDefaultExtension /// <summary> /// This tool generates code, and the default extension equals that of the current code provider. /// </summary> public override string GetDefaultExtension() { return "Serialization." + base.CodeProvider.FileExtension; } #endregion GetDefaultExtension #region Registration and Installation /// <summary> /// Registers the generator. /// </summary> [ComRegisterFunction] public static void RegisterClass(Type type) { CustomTool.Register(typeof(SGenTool)); } /// <summary> /// Unregisters the generator. /// </summary> [ComUnregisterFunction] public static void UnregisterClass(Type t) { CustomTool.UnRegister(typeof(SGenTool)); } #endregion Registration and Installation } } |