LinkXmlWriter.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using dnlib.DotNet;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace HybridCLR.Editor.Link
  10. {
  11. internal class LinkXmlWriter
  12. {
  13. public void Write(string outputLinkXmlFile, HashSet<TypeRef> refTypes)
  14. {
  15. string parentDir = Directory.GetParent(outputLinkXmlFile).FullName;
  16. Directory.CreateDirectory(parentDir);
  17. var writer = System.Xml.XmlWriter.Create(outputLinkXmlFile,
  18. new System.Xml.XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true});
  19. writer.WriteStartDocument();
  20. writer.WriteStartElement("linker");
  21. var typesByAssembly = refTypes.GroupBy(t => t.DefinitionAssembly.Name.String).ToList();
  22. typesByAssembly.Sort((a, b) => String.Compare(a.Key, b.Key, StringComparison.Ordinal));
  23. foreach(var assembly in typesByAssembly)
  24. {
  25. writer.WriteStartElement("assembly");
  26. writer.WriteAttributeString("fullname", assembly.Key);
  27. List<string> assTypeNames = assembly.Select(t => t.FullName).ToList();
  28. assTypeNames.Sort(string.CompareOrdinal);
  29. foreach(var typeName in assTypeNames)
  30. {
  31. writer.WriteStartElement("type");
  32. writer.WriteAttributeString("fullname", typeName);
  33. writer.WriteAttributeString("preserve", "all");
  34. writer.WriteEndElement();
  35. }
  36. writer.WriteEndElement();
  37. }
  38. writer.WriteEndElement();
  39. writer.WriteEndDocument();
  40. writer.Close();
  41. }
  42. }
  43. }