ExtensibleStorageUtil.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:ExtensibleStorageUtil
  3. * 作者:xulisong
  4. * 创建时间: 2019/7/1 9:39:17
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using Autodesk.Revit.DB;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using Autodesk.Revit.DB.ExtensibleStorage;
  16. namespace FWindSoft.Revit.ExtensibleStorage
  17. {
  18. /// <summary>
  19. /// 扩展存储工具类
  20. /// </summary>
  21. public class ExtensibleStorageUtil
  22. {
  23. /// <summary>
  24. /// 扩展数据内容兼容类型
  25. /// </summary>
  26. private static HashSet<Type> ContentTypes { get; set; } = new HashSet<Type>();
  27. /// <summary>
  28. /// 扩展数据键值兼容类型
  29. /// </summary>
  30. private static HashSet<Type> KeyTypes { get; set; } = new HashSet<Type>();
  31. static ExtensibleStorageUtil()
  32. {
  33. InitData();
  34. }
  35. private static void InitData()
  36. {
  37. #region 内容类型
  38. ContentTypes.Add(typeof(bool));
  39. ContentTypes.Add(typeof(byte));
  40. ContentTypes.Add(typeof(Int16));
  41. ContentTypes.Add(typeof(Int32));
  42. ContentTypes.Add(typeof(float));
  43. ContentTypes.Add(typeof(double));
  44. ContentTypes.Add(typeof(ElementId));
  45. ContentTypes.Add(typeof(Guid));
  46. ContentTypes.Add(typeof(string));
  47. ContentTypes.Add(typeof(XYZ));
  48. ContentTypes.Add(typeof(UV));
  49. ContentTypes.Add(typeof(Entity));
  50. #endregion
  51. #region 键值类型
  52. KeyTypes.Add(typeof(bool));
  53. KeyTypes.Add(typeof(byte));
  54. KeyTypes.Add(typeof(Int16));
  55. KeyTypes.Add(typeof(Int32));
  56. KeyTypes.Add(typeof(ElementId));
  57. KeyTypes.Add(typeof(Guid));
  58. KeyTypes.Add(typeof(string));
  59. #endregion
  60. }
  61. /// <summary>
  62. /// 判定是否是有效的content类型
  63. /// </summary>
  64. /// <param name="type"></param>
  65. /// <returns></returns>
  66. public static bool IsEffectiveContent(Type type)
  67. {
  68. return ContentTypes.Contains(type);
  69. }
  70. /// <summary>
  71. /// 判定是否是有效的key类型
  72. /// </summary>
  73. /// <param name="type"></param>
  74. /// <returns></returns>
  75. public static bool IsEffectiveKey(Type type)
  76. {
  77. return KeyTypes.Contains(type);
  78. }
  79. public static bool IsEffectiveArray(Type type,out Type contentType)
  80. {
  81. contentType = null;
  82. var baseType = type.GetInterface(typeof(IList<>).FullName);
  83. if (baseType == null)
  84. {
  85. return false;
  86. }
  87. var firstType = baseType.GetGenericArguments()?.FirstOrDefault();
  88. var flag= firstType != null && IsEffectiveContent(firstType);
  89. if (flag)
  90. {
  91. contentType = firstType;
  92. }
  93. return flag;
  94. }
  95. public static bool IsEffectiveMap(Type type,out Type outKeyType,out Type outValueTYpe)
  96. {
  97. outKeyType = null;
  98. outValueTYpe = null;
  99. var baseType = type.GetInterface(typeof(IDictionary<,>).FullName);
  100. if (baseType == null)
  101. {
  102. return false;
  103. }
  104. var keyType = baseType.GetGenericArguments()?.FirstOrDefault();
  105. var valueType = baseType.GetGenericArguments()?.FirstOrDefault();
  106. var flag= keyType != null && valueType != null && IsEffectiveKey(keyType) && IsEffectiveContent(valueType);
  107. if (flag)
  108. {
  109. outKeyType = keyType;
  110. outValueTYpe = valueType;
  111. }
  112. return flag;
  113. }
  114. /// <summary>
  115. /// 是否为自定义解析存储
  116. /// </summary>
  117. /// <param name="type"></param>
  118. /// <returns></returns>
  119. public static bool IsCoustomStorage(Type type)
  120. {
  121. var baseType = type.GetInterface(typeof(ICoustomStorage).FullName);
  122. return baseType != null;
  123. }
  124. /// <summary>
  125. /// 获取可用set方法
  126. /// </summary>
  127. /// <returns></returns>
  128. public static MethodInfo GetEntitySetMethod()
  129. {
  130. var methods= typeof(Entity).GetMethods(BindingFlags.Public | BindingFlags.Instance);
  131. foreach (var methodInfo in methods)
  132. {
  133. if (methodInfo.Name != "Set")
  134. {
  135. continue;
  136. }
  137. var parameters = methodInfo.GetParameters();
  138. if (parameters.Length == 2 && parameters[0].ParameterType == typeof(string))
  139. {
  140. return methodInfo;
  141. }
  142. }
  143. return null;
  144. }
  145. public static Type GetFileType(Field field)
  146. {
  147. var useType = field.ValueType;
  148. if (field.ContainerType == ContainerType.Array)
  149. {
  150. useType = typeof(IList<>).MakeGenericType(field.ValueType);
  151. }
  152. else if (field.ContainerType == ContainerType.Map)
  153. {
  154. useType = typeof(IDictionary<,>).MakeGenericType(field.KeyType, field.ValueType);
  155. }
  156. return useType;
  157. }
  158. }
  159. }