using Apewer; using Apewer.Internals.QrCode; using Apewer.Models; using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace Apewer { /// 类实用工具。 public class ClassUtility { /// 从外部加载对象。 public static object CreateObject(string path, string type, bool ignoreCase) { try { var a = Assembly.LoadFrom(path); var t = a.GetType(type, false, ignoreCase); if (t != null) { var result = Activator.CreateInstance(t); return result; } } catch { } return null; } /// 解析源对象。 public static Dictionary GetOrigin(TextSet instance) { if (instance == null) return null; return instance.Origin; } /// 解析源对象。 public static Dictionary GetOrigin(ObjectSet instance) { if (instance == null) return null; return instance.Origin; } /// 解析源对象。 public static Dictionary GetOrigin(ObjectSet instance) // where T : ObjectSet { if (instance == null) return null; return instance.Origin; } /// 获取指定的首个特性,特性不存在时返回 Null 值。 public static T GetAttribute(object target) where T : Attribute { if (target == null) return null; try { return GetAttribute(target.GetType()); } catch { return null; } } /// 获取指定的首个特性,特性不存在时返回 Null 值。 public static T GetAttribute(Type target, bool inherit = false) where T : Attribute { if (target == null) return null; try { var type = target; var attributes = type.GetCustomAttributes(typeof(T), inherit); if (attributes.LongLength > 0L) { var attribute = attributes[0] as T; return attribute; } } catch { } return null; } /// 获取指定的首个特性,特性不存在时返回 Null 值。 public static T GetAttribute(PropertyInfo property, bool inherit = false) where T : Attribute { if (property == null) return null; try { var type = property; var attributes = type.GetCustomAttributes(typeof(T), inherit); if (attributes.LongLength > 0L) { var attribute = attributes[0] as T; return attribute; } } catch { } return null; } /// 获取指定的首个特性,特性不存在时返回 Null 值。 public static T GetAttribute(MethodInfo methods, bool inherit = false) where T : Attribute { if (methods == null) return null; try { var type = methods; var attributes = type.GetCustomAttributes(typeof(T), inherit); if (attributes.LongLength > 0L) { var attribute = attributes[0] as T; return attribute; } } catch { } return null; } /// 获取一个值,指示该属性是否 static。 public static bool IsStatic(PropertyInfo property, bool withNonPublic = false) { if (property == null) return false; var getter = property.GetGetMethod(); if (getter != null) return getter.IsStatic; if (withNonPublic) { getter = property.GetGetMethod(true); if (getter != null) return getter.IsStatic; } var setter = property.GetSetMethod(); if (setter != null) return setter.IsStatic; if (withNonPublic) { setter = property.GetSetMethod(true); if (setter != null) return setter.IsStatic; } return false; } /// 调用泛型对象的 ToString() 方法。 public static string ToString(T target) { var type = typeof(T); var methods = type.GetMethods(); foreach (var method in methods) { if (method.Name != "ToString") continue; if (method.GetParameters().LongLength > 0L) continue; var result = (string)method.Invoke(target, null); return result; } return null; } /// 调用方法。 /// /// /// /// /// /// /// public static object InvokeMethod(object instance, MethodInfo method, params object[] parameters) { if (instance == null || method == null) return null; { var pis = method.GetParameters(); if (pis == null || pis.Length < 1) { return method.Invoke(instance, null); } } { if (parameters == null) return method.Invoke(instance, new object[] { null }); return method.Invoke(instance, parameters); } } /// 获取属性值。 /// /// /// /// /// /// /// public static T InvokeGet(object instance, PropertyInfo property) { if (instance == null || property == null || !property.CanRead) return default; #if NET20 || NET40 var getter = property.GetGetMethod(); if (getter != null) { try { var value = getter.Invoke(instance, null); if (value != null) { return (T)value; } } catch { } } return default; #else var value = property.GetValue(instance); try { return (T)value; } catch { return default; } #endif } /// 设置属性值。 /// /// /// /// /// /// /// public static void InvokeSet(object instance, PropertyInfo property, T value) { if (instance == null || property == null || !property.CanWrite) return; #if NET20 || NET40 var setter = property.GetSetMethod(); if (setter != null) { try { setter.Invoke(instance, new object[] { value }); } catch { } } #else property.SetValue(instance, value); #endif } /// 指示方法是否存在于指定类型中。 public static bool ExistIn(MethodInfo method, Type type) { if (method == null) return false; if (type == null) return false; return type.Equals(method.DeclaringType.Equals(type)); } /// 指示方法是否存在于指定类型中。 public static bool ExistIn(MethodInfo method) { if (method == null) return false; var type = typeof(T); return type.Equals(method.DeclaringType.Equals(type)); } /// 判断指定类型可序列化。 public static bool CanSerialize(Type type, bool inherit = false) { if (type == null) return false; var nsas = type.GetCustomAttributes(typeof(NonSerializedAttribute), inherit); if (nsas != null && nsas.Length > 0) return false; var sas = type.GetCustomAttributes(typeof(SerializableAttribute), inherit); if (sas == null || sas.Length < 1) return false; return true; } /// 判断指定类型具有特性。 private static bool ContainsAttribute(object[] attributes) where T : Attribute { if (attributes == null) return false; if (attributes.Length < 1) return false; return true; } /// 判断指定类型具有特性。 public static bool ContainsAttribute(Type type, bool inherit = false) where T : Attribute { return type == null ? false : ContainsAttribute(type.GetCustomAttributes(typeof(T), inherit)); } /// 判断指定属性具有特性。 public static bool ContainsAttribute(PropertyInfo property, bool inherit = false) where T : Attribute { return property == null ? false : ContainsAttribute(property.GetCustomAttributes(typeof(T), inherit)); } /// 判断基类。 public static bool IsInherits(Type child, Type @base) { // 检查参数。 if (child == null || @base == null) return false; // 忽略 System.Object。 var quantum = typeof(object); if (@base.Equals(quantum)) return true; if (child.Equals(quantum)) return true; // 循环判断基类。 var current = child; while (true) { var parent = current.BaseType; if (parent.Equals(@base)) return true; if (parent.Equals(quantum)) break; current = parent; } return false; } /// public static IEnumerable GetTypes(Assembly assembly) { if (assembly == null) return null; var types = null as IEnumerable; if (types == null) { try { types = assembly.GetExportedTypes(); } catch { types = null; } } if (types == null) { try { types = assembly.GetTypes(); } catch { types = null; } } return types; } /// 能从外部创建对象实例。 public static bool CanNewClass() { return CanNewClass(typeof(T)); } /// 能从外部创建对象实例。 public static bool CanNewClass(Type type) { if (type == null) return false; if (!type.IsClass) return false; if (type.IsAbstract) return false; var methods = type.GetMethods(); var constructors = type.GetConstructors(); foreach (var i in constructors) { if (i.IsPublic) { var parameters = i.GetParameters(); if (parameters.Length < 1) { return true; } } } return false; } /// 克隆对象,创建新对象。可指定包含对象的属性和字段。 public static T Clone(T current, T failed = default(T), bool properties = true, bool fields = false) where T : new() { if (current == null) return default(T); var type = typeof(T); var target = new T(); foreach (var property in type.GetProperties()) { var getter = property.GetGetMethod(); var setter = property.GetSetMethod(); if (getter == null || setter == null) continue; var value = getter.Invoke(current, null); setter.Invoke(target, new object[] { value }); } foreach (var field in type.GetFields()) { var value = field.GetValue(current); field.SetValue(target, value); } return target; } /// 对每个属性执行动作。 public static void ForEachProperties(object instance, Action action, bool withNonPublic = true, bool withStatic = false) { if (instance == null || action == null) return; var type = instance.GetType(); if (type == null) return; var properties = new List(); properties.AddRange(type.GetProperties()); if (withNonPublic) properties.AddRange(type.GetProperties(BindingFlags.NonPublic)); foreach (var info in properties) { var arg = new Property() { Instance = instance, Type = type, Information = info, Getter = info.GetGetMethod(), Setter = info.GetSetMethod() }; if (arg.IsStatic && !withStatic) continue; action.Invoke(arg); } } /// 遍历属性。 public static void ForEachPublicProperties(object instance, Action action) { if (instance != null && action != null) ForEachPublicProperties(instance.GetType(), action); } /// 遍历属性。 public static void ForEachPublicProperties(Type type, Action action) { if (type == null || action == null) return; foreach (var property in type.GetProperties()) action.Invoke(property); } /// 检查类型。 public static bool TypeEquals(object instance, Type type) { if (instance == null) return type == null ? true : false; if (instance is PropertyInfo) { return ((PropertyInfo)instance).PropertyType.Equals(type); } if (instance is MethodInfo) { return ((MethodInfo)instance).ReturnType.Equals(type); } return instance.GetType().Equals(type); } /// 检查类型。 public static bool TypeEquals(object instance) { return TypeEquals(instance, typeof(T)); } /// 安全转换为 List<> 对象。可指定排除 NULL 值元素。 /// public static List ToList(IEnumerable objects, bool excludeNull = false) { var list = new List(); if (objects != null) { foreach (var value in objects) { if (excludeNull && value == null) continue; list.Add(value); } } return list; } /// public static bool IsEmpty(IEnumerable objects) { if (objects == null) return true; foreach (var i in objects) return false; return true; } /// public static bool NotEmpty(IEnumerable objects) { if (objects == null) return false; foreach (var i in objects) return true; return false; } /// public static bool Contains(IEnumerable objects, T cell) { if (objects == null) return false; var a = (object)cell; foreach (var i in objects) { var b = (object)i; if (a == b) return true; } return false; } /// public static List Sub(IEnumerable objects, long start = 0, long count = -1, Func stuffer = null) { if (count == 0) return new List(); var hasCount = count > 0L; var output = new List(); if (start < 0) { for (var i = start; i < 0; i++) { output.Add(stuffer == null ? default : stuffer()); if (hasCount && output.Count >= count) return output; } } if (objects != null) { var offset = 0L; foreach (var i in objects) { if (offset < start) { offset += 1L; continue; } offset += 1L; output.Add(i); if (hasCount && output.Count >= count) return output; } } while (hasCount && output.Count < count) { output.Add(stuffer == null ? default : stuffer()); } return output; } } }