using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace IFramework
{
public static class Ex
{
///
/// 是否存在文件
///
///
///
public static bool ExistFile(this string path)
{
return File.Exists(path);
}
///
/// 是否是一个文件夹
///
///
///
public static bool IsDirectory(this string path)
{
FileInfo fi = new FileInfo(path);
if ((fi.Attributes & FileAttributes.Directory) != 0)
return true;
return false;
}
///
/// 移除空文件夹
///
///
///
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;
}
}
///
/// 拼接路径
///
///
///
///
public static string CombinePath(this string path, string toCombinePath)
{
return Path.Combine(path, toCombinePath).ToRegularPath();
}
///
/// 拼接路径
///
///
///
///
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();
}
///
/// 规范路径
///
///
///
public static string ToRegularPath(this string path)
{
path = path.Replace('\\', '/');
return path;
}
///
/// 如果文件夹不存在则创建
///
///
public static void MakeDirectoryExist(this string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
///
/// 获取当前程序集中的类型的子类,3.5有问题
///
///
///
public static IEnumerable 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));
}
///
/// 获取所有程序集中的类型的子类,3.5有问题
///
///
///
public static IEnumerable 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));
}
///
/// 是否继承接口
///
///
///
///
public static bool IsExtendInterface(this Type self, Type Interface)
{
return self.GetInterfaces().Contains(Interface);
}
///
/// 是否继承自泛型类
///
///
///
///
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;
}
///
/// 获取类型树
///
///
///
public static IList GetTypeTree(this Type t)
{
var tmp = t;
var types = new List();
do
{
types.Add(t);
t = t.BaseType;
} while (t != null);
types.AddRange(tmp.GetInterfaces());
return types;
}
///
/// 获取程序集下的静态扩展
///
///
///
///
public static IEnumerable 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;
}
///
/// 字符串结尾转Unix编码
///
///
///
public static string ToUnixLineEndings(this string self)
{
return self.Replace("\r\n", "\n").Replace("\r", "\n");
}
///
/// 在字符串前拼接字符串
///
///
///
///
public static string AppendHead(this string self, string toPrefix)
{
return new StringBuilder(toPrefix).Append(self).ToString();
}
///
/// 拼接字符串
///
///
///
///
public static string Append(this string self, string toAppend)
{
return new StringBuilder(self).Append(toAppend).ToString();
}
///
/// 拼接字符串
///
///
///
///
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();
}
}
}