MenuConfigParser.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:MenuConfigParser
  3. * 作者:xulisong
  4. * 创建时间: 2019/3/12 14:43:05
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Xml;
  16. using FWindSoft.Tools;
  17. namespace FWindSoft.Revit.Menu
  18. {
  19. public class MenuConfigParser
  20. {
  21. public static MenuConfig Load(string path)
  22. {
  23. XmlDocument xmlDoc = new XmlDocument();
  24. xmlDoc.Load(path);
  25. return Load(xmlDoc);
  26. }
  27. public static MenuConfig LoadContent(string xmlContent)
  28. {
  29. XmlDocument xmlDoc = new XmlDocument();
  30. xmlDoc.LoadXml(xmlContent);
  31. return Load(xmlDoc);
  32. }
  33. public static MenuConfig Load(Stream steam)
  34. {
  35. XmlDocument xmlDoc = new XmlDocument();
  36. xmlDoc.Load(steam);
  37. return Load(xmlDoc);
  38. }
  39. public static MenuConfig Load(XmlDocument xmlDocument)
  40. {
  41. MenuConfig config = new MenuConfig();
  42. var root = xmlDocument.SelectSingleNode(@"Menu");
  43. if (root == null)
  44. {
  45. return config;
  46. }
  47. Dictionary<string, string> map = new Dictionary<string, string>();
  48. map["Tabs"] = @"Tabs/Tab";
  49. map["Panels"] = @"Panels/Panel";
  50. map["Buttons"] = @"Buttons/Button";
  51. #region 解析tab
  52. var configProperties = typeof(MenuConfig).GetProperties(BindingFlags.Instance | BindingFlags.Public);
  53. foreach (var keyValue in map)
  54. {
  55. var property = configProperties.FirstOrDefault(p => p.Name == keyValue.Key);
  56. if (property == null)
  57. {
  58. continue;
  59. }
  60. var nodes = root.SelectNodes(keyValue.Value);
  61. if (nodes != null)
  62. {
  63. foreach (XmlNode node in nodes)
  64. {
  65. var revitObject = CreateObject(node,property.PropertyType.GenericTypeArguments[0]);
  66. ((IList) property.GetValue(config)).Add(revitObject);
  67. }
  68. }
  69. }
  70. #endregion
  71. return config;
  72. }
  73. #region 解析节点通用方法
  74. private static T CreateObject<T>(XmlNode xmlNode)
  75. {
  76. T t = Activator.CreateInstance<T>();
  77. PropertyInfo[] properties = t.GetType().GetProperties();
  78. for (int i = 0; i < properties.Length; i++)
  79. {
  80. PropertyInfo propertyInfo = properties[i];
  81. try
  82. {
  83. var currentNode = xmlNode.SelectSingleNode(string.Format("/{0}", propertyInfo.Name));
  84. if (currentNode != null)
  85. {
  86. propertyInfo.SetValue(t, TypeUtil.ChangeType(currentNode.InnerText, propertyInfo.PropertyType) ?? "", null);
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. }
  92. }
  93. return t;
  94. }
  95. private static object CreateObject(XmlNode xmlNode,Type targetType)
  96. {
  97. object t = Activator.CreateInstance(targetType);
  98. PropertyInfo[] properties = t.GetType().GetProperties();
  99. for (int i = 0; i < properties.Length; i++)
  100. {
  101. PropertyInfo propertyInfo = properties[i];
  102. try
  103. {
  104. var currentNode = xmlNode.SelectSingleNode(string.Format("{0}", propertyInfo.Name));
  105. if (currentNode != null)
  106. {
  107. propertyInfo.SetValue(t, TypeUtil.ChangeType(currentNode.InnerText, propertyInfo.PropertyType) ?? "", null);
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. }
  113. }
  114. return t;
  115. }
  116. #endregion
  117. }
  118. }