123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace IFramework
- {
- public static class Ex
- {
- /// <summary>
- /// 是否存在文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool ExistFile(this string path)
- {
- return File.Exists(path);
- }
- /// <summary>
- /// 是否是一个文件夹
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool IsDirectory(this string path)
- {
- FileInfo fi = new FileInfo(path);
- if ((fi.Attributes & FileAttributes.Directory) != 0)
- return true;
- return false;
- }
- /// <summary>
- /// 移除空文件夹
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool RemoveEmptyDirectory(this string path)
- {
- if (string.IsNullOrEmpty(path)) throw new Exception("Directory name is invalid.");
- try
- {
- if (!Directory.Exists(path)) return false;
- string[] subDirectoryNames = Directory.GetDirectories(path, "*");
- int subDirectoryCount = subDirectoryNames.Length;
- foreach (string subDirectoryName in subDirectoryNames)
- {
- if (RemoveEmptyDirectory(subDirectoryName))
- {
- subDirectoryCount--;
- }
- }
- if (subDirectoryCount > 0) return false;
- if (Directory.GetFiles(path, "*").Length > 0) return false;
- Directory.Delete(path);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 拼接路径
- /// </summary>
- /// <param name="path"></param>
- /// <param name="toCombinePath"></param>
- /// <returns></returns>
- public static string CombinePath(this string path, string toCombinePath)
- {
- return Path.Combine(path, toCombinePath).ToRegularPath();
- }
- /// <summary>
- /// 拼接路径
- /// </summary>
- /// <param name="path"></param>
- /// <param name="paths"></param>
- /// <returns></returns>
- public static string CombinePath(this string path, string[] paths)
- {
- for (int i = 1; i < paths.Length; i++)
- {
- path = path.CombinePath(paths[i]);
- }
- return path.ToRegularPath();
- }
- /// <summary>
- /// 规范路径
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string ToRegularPath(this string path)
- {
- path = path.Replace('\\', '/');
- return path;
- }
- /// <summary>
- /// 如果文件夹不存在则创建
- /// </summary>
- /// <param name="path"></param>
- public static void MakeDirectoryExist(this string path)
- {
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- }
- /// <summary>
- /// 获取当前程序集中的类型的子类,3.5有问题
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static IEnumerable<Type> GetSubTypesInAssembly(this Type self)
- {
- if (self.IsInterface)
- return Assembly.GetExecutingAssembly()
- .GetTypes()
- .Where(item => item.GetInterfaces().Contains(self));
- return Assembly.GetExecutingAssembly()
- .GetTypes()
- .Where(item => item.IsSubclassOf(self));
- }
- /// <summary>
- /// 获取所有程序集中的类型的子类,3.5有问题
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static IEnumerable<Type> GetSubTypesInAssemblys(this Type self)
- {
- if (self.IsInterface)
- return AppDomain.CurrentDomain.GetAssemblies()
- .SelectMany(item => item.GetTypes())
- .Where(item => item.GetInterfaces().Contains(self));
- return AppDomain.CurrentDomain.GetAssemblies()
- .SelectMany(item => item.GetTypes())
- .Where(item => item.IsSubclassOf(self));
- }
- /// <summary>
- /// 是否继承接口
- /// </summary>
- /// <param name="self"></param>
- /// <param name="Interface"></param>
- /// <returns></returns>
- public static bool IsExtendInterface(this Type self, Type Interface)
- {
- return self.GetInterfaces().Contains(Interface);
- }
- /// <summary>
- /// 是否继承自泛型类
- /// </summary>
- /// <param name="self"></param>
- /// <param name="genericType"></param>
- /// <returns></returns>
- public static bool IsSubclassOfGeneric(this Type self, Type genericType)
- {
- #if NETFX_CORE
- if (!genericTypeDefinition.GetTypeInfo().IsGenericTypeDefinition)
- #else
- if (!genericType.IsGenericTypeDefinition)
- #endif
- return false;
- #if NETFX_CORE
- if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition().Equals(genericTypeDefinition))
- #else
- if (self.IsGenericType && self.GetGenericTypeDefinition().Equals(genericType))
- #endif
- return true;
- #if NETFX_CORE
- Type baseType = type.GetTypeInfo().BaseType;
- #else
- Type baseType = self.BaseType;
- #endif
- if (baseType != null && baseType != typeof(object))
- {
- if (IsSubclassOfGeneric(baseType, genericType))
- return true;
- }
- foreach (Type t in self.GetInterfaces())
- {
- if (IsSubclassOfGeneric(t, genericType))
- return true;
- }
- return false;
- }
- /// <summary>
- /// 获取类型树
- /// </summary>
- /// <param name="t"></param>
- /// <returns></returns>
- public static IList<Type> GetTypeTree(this Type t)
- {
- var tmp = t;
- var types = new List<Type>();
- do
- {
- types.Add(t);
- t = t.BaseType;
- } while (t != null);
- types.AddRange(tmp.GetInterfaces());
- return types;
- }
- /// <summary>
- /// 获取程序集下的静态扩展
- /// </summary>
- /// <param name="self"></param>
- /// <param name="assembly"></param>
- /// <returns></returns>
- public static IEnumerable<MethodInfo> GetExtensionMethods(this Type self, Assembly assembly)
- {
- var query = from type in assembly.GetTypes()
- where !type.IsGenericType && !type.IsNested
- from method in type.GetMethods(BindingFlags.Static
- | BindingFlags.Public | BindingFlags.NonPublic)
- where method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false)
- where method.GetParameters()[0].ParameterType == self
- select method;
- return query;
- }
- /// <summary>
- /// 字符串结尾转Unix编码
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static string ToUnixLineEndings(this string self)
- {
- return self.Replace("\r\n", "\n").Replace("\r", "\n");
- }
- /// <summary>
- /// 在字符串前拼接字符串
- /// </summary>
- /// <param name="self"></param>
- /// <param name="toPrefix"></param>
- /// <returns></returns>
- public static string AppendHead(this string self, string toPrefix)
- {
- return new StringBuilder(toPrefix).Append(self).ToString();
- }
- /// <summary>
- /// 拼接字符串
- /// </summary>
- /// <param name="self"></param>
- /// <param name="toAppend"></param>
- /// <returns></returns>
- public static string Append(this string self, string toAppend)
- {
- return new StringBuilder(self).Append(toAppend).ToString();
- }
- /// <summary>
- /// 拼接字符串
- /// </summary>
- /// <param name="self"></param>
- /// <param name="toAppend"></param>
- /// <returns></returns>
- public static string Append(this string self, params string[] toAppend)
- {
- if (toAppend == null)
- {
- return self;
- }
- StringBuilder result = new StringBuilder(self);
- foreach (string str in toAppend)
- {
- result = result.Append(str);
- }
- return result.ToString();
- }
- }
- }
|