Ex.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace IFramework
  8. {
  9. public static class Ex
  10. {
  11. /// <summary>
  12. /// 是否存在文件
  13. /// </summary>
  14. /// <param name="path"></param>
  15. /// <returns></returns>
  16. public static bool ExistFile(this string path)
  17. {
  18. return File.Exists(path);
  19. }
  20. /// <summary>
  21. /// 是否是一个文件夹
  22. /// </summary>
  23. /// <param name="path"></param>
  24. /// <returns></returns>
  25. public static bool IsDirectory(this string path)
  26. {
  27. FileInfo fi = new FileInfo(path);
  28. if ((fi.Attributes & FileAttributes.Directory) != 0)
  29. return true;
  30. return false;
  31. }
  32. /// <summary>
  33. /// 移除空文件夹
  34. /// </summary>
  35. /// <param name="path"></param>
  36. /// <returns></returns>
  37. public static bool RemoveEmptyDirectory(this string path)
  38. {
  39. if (string.IsNullOrEmpty(path)) throw new Exception("Directory name is invalid.");
  40. try
  41. {
  42. if (!Directory.Exists(path)) return false;
  43. string[] subDirectoryNames = Directory.GetDirectories(path, "*");
  44. int subDirectoryCount = subDirectoryNames.Length;
  45. foreach (string subDirectoryName in subDirectoryNames)
  46. {
  47. if (RemoveEmptyDirectory(subDirectoryName))
  48. {
  49. subDirectoryCount--;
  50. }
  51. }
  52. if (subDirectoryCount > 0) return false;
  53. if (Directory.GetFiles(path, "*").Length > 0) return false;
  54. Directory.Delete(path);
  55. return true;
  56. }
  57. catch
  58. {
  59. return false;
  60. }
  61. }
  62. /// <summary>
  63. /// 拼接路径
  64. /// </summary>
  65. /// <param name="path"></param>
  66. /// <param name="toCombinePath"></param>
  67. /// <returns></returns>
  68. public static string CombinePath(this string path, string toCombinePath)
  69. {
  70. return Path.Combine(path, toCombinePath).ToRegularPath();
  71. }
  72. /// <summary>
  73. /// 拼接路径
  74. /// </summary>
  75. /// <param name="path"></param>
  76. /// <param name="paths"></param>
  77. /// <returns></returns>
  78. public static string CombinePath(this string path, string[] paths)
  79. {
  80. for (int i = 1; i < paths.Length; i++)
  81. {
  82. path = path.CombinePath(paths[i]);
  83. }
  84. return path.ToRegularPath();
  85. }
  86. /// <summary>
  87. /// 规范路径
  88. /// </summary>
  89. /// <param name="path"></param>
  90. /// <returns></returns>
  91. public static string ToRegularPath(this string path)
  92. {
  93. path = path.Replace('\\', '/');
  94. return path;
  95. }
  96. /// <summary>
  97. /// 如果文件夹不存在则创建
  98. /// </summary>
  99. /// <param name="path"></param>
  100. public static void MakeDirectoryExist(this string path)
  101. {
  102. if (!Directory.Exists(path))
  103. {
  104. Directory.CreateDirectory(path);
  105. }
  106. }
  107. /// <summary>
  108. /// 获取当前程序集中的类型的子类,3.5有问题
  109. /// </summary>
  110. /// <param name="self"></param>
  111. /// <returns></returns>
  112. public static IEnumerable<Type> GetSubTypesInAssembly(this Type self)
  113. {
  114. if (self.IsInterface)
  115. return Assembly.GetExecutingAssembly()
  116. .GetTypes()
  117. .Where(item => item.GetInterfaces().Contains(self));
  118. return Assembly.GetExecutingAssembly()
  119. .GetTypes()
  120. .Where(item => item.IsSubclassOf(self));
  121. }
  122. /// <summary>
  123. /// 获取所有程序集中的类型的子类,3.5有问题
  124. /// </summary>
  125. /// <param name="self"></param>
  126. /// <returns></returns>
  127. public static IEnumerable<Type> GetSubTypesInAssemblys(this Type self)
  128. {
  129. if (self.IsInterface)
  130. return AppDomain.CurrentDomain.GetAssemblies()
  131. .SelectMany(item => item.GetTypes())
  132. .Where(item => item.GetInterfaces().Contains(self));
  133. return AppDomain.CurrentDomain.GetAssemblies()
  134. .SelectMany(item => item.GetTypes())
  135. .Where(item => item.IsSubclassOf(self));
  136. }
  137. /// <summary>
  138. /// 是否继承接口
  139. /// </summary>
  140. /// <param name="self"></param>
  141. /// <param name="Interface"></param>
  142. /// <returns></returns>
  143. public static bool IsExtendInterface(this Type self, Type Interface)
  144. {
  145. return self.GetInterfaces().Contains(Interface);
  146. }
  147. /// <summary>
  148. /// 是否继承自泛型类
  149. /// </summary>
  150. /// <param name="self"></param>
  151. /// <param name="genericType"></param>
  152. /// <returns></returns>
  153. public static bool IsSubclassOfGeneric(this Type self, Type genericType)
  154. {
  155. #if NETFX_CORE
  156. if (!genericTypeDefinition.GetTypeInfo().IsGenericTypeDefinition)
  157. #else
  158. if (!genericType.IsGenericTypeDefinition)
  159. #endif
  160. return false;
  161. #if NETFX_CORE
  162. if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition().Equals(genericTypeDefinition))
  163. #else
  164. if (self.IsGenericType && self.GetGenericTypeDefinition().Equals(genericType))
  165. #endif
  166. return true;
  167. #if NETFX_CORE
  168. Type baseType = type.GetTypeInfo().BaseType;
  169. #else
  170. Type baseType = self.BaseType;
  171. #endif
  172. if (baseType != null && baseType != typeof(object))
  173. {
  174. if (IsSubclassOfGeneric(baseType, genericType))
  175. return true;
  176. }
  177. foreach (Type t in self.GetInterfaces())
  178. {
  179. if (IsSubclassOfGeneric(t, genericType))
  180. return true;
  181. }
  182. return false;
  183. }
  184. /// <summary>
  185. /// 获取类型树
  186. /// </summary>
  187. /// <param name="t"></param>
  188. /// <returns></returns>
  189. public static IList<Type> GetTypeTree(this Type t)
  190. {
  191. var tmp = t;
  192. var types = new List<Type>();
  193. do
  194. {
  195. types.Add(t);
  196. t = t.BaseType;
  197. } while (t != null);
  198. types.AddRange(tmp.GetInterfaces());
  199. return types;
  200. }
  201. /// <summary>
  202. /// 获取程序集下的静态扩展
  203. /// </summary>
  204. /// <param name="self"></param>
  205. /// <param name="assembly"></param>
  206. /// <returns></returns>
  207. public static IEnumerable<MethodInfo> GetExtensionMethods(this Type self, Assembly assembly)
  208. {
  209. var query = from type in assembly.GetTypes()
  210. where !type.IsGenericType && !type.IsNested
  211. from method in type.GetMethods(BindingFlags.Static
  212. | BindingFlags.Public | BindingFlags.NonPublic)
  213. where method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false)
  214. where method.GetParameters()[0].ParameterType == self
  215. select method;
  216. return query;
  217. }
  218. /// <summary>
  219. /// 字符串结尾转Unix编码
  220. /// </summary>
  221. /// <param name="self"></param>
  222. /// <returns></returns>
  223. public static string ToUnixLineEndings(this string self)
  224. {
  225. return self.Replace("\r\n", "\n").Replace("\r", "\n");
  226. }
  227. /// <summary>
  228. /// 在字符串前拼接字符串
  229. /// </summary>
  230. /// <param name="self"></param>
  231. /// <param name="toPrefix"></param>
  232. /// <returns></returns>
  233. public static string AppendHead(this string self, string toPrefix)
  234. {
  235. return new StringBuilder(toPrefix).Append(self).ToString();
  236. }
  237. /// <summary>
  238. /// 拼接字符串
  239. /// </summary>
  240. /// <param name="self"></param>
  241. /// <param name="toAppend"></param>
  242. /// <returns></returns>
  243. public static string Append(this string self, string toAppend)
  244. {
  245. return new StringBuilder(self).Append(toAppend).ToString();
  246. }
  247. /// <summary>
  248. /// 拼接字符串
  249. /// </summary>
  250. /// <param name="self"></param>
  251. /// <param name="toAppend"></param>
  252. /// <returns></returns>
  253. public static string Append(this string self, params string[] toAppend)
  254. {
  255. if (toAppend == null)
  256. {
  257. return self;
  258. }
  259. StringBuilder result = new StringBuilder(self);
  260. foreach (string str in toAppend)
  261. {
  262. result = result.Append(str);
  263. }
  264. return result.ToString();
  265. }
  266. }
  267. }