using System.ComponentModel;
using System.Reflection;
namespace DS.Module.Core.Extensions;
public static partial class Extensions
{
///
/// 判断类型是否为Nullable类型
///
/// 要处理的类型
/// 是返回True,不是返回False
public static bool IsNullableType(this Type type)
{
return ((type != null) && type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
///
/// 判断当前类型是否可由指定类型派生
///
///
///
///
///
public static bool IsDeriveClassFrom(this Type type, bool canAbstract = false)
{
return IsDeriveClassFrom(type, typeof(TBaseType), canAbstract);
}
///
/// 判断当前类型是否可由指定类型派生
///
public static bool IsDeriveClassFrom(this Type type, Type baseType, bool canAbstract = false)
{
type.NotNull(nameof(type));
baseType.NotNull(nameof(baseType));
return type.IsClass && (!canAbstract && !type.IsAbstract) && type.IsBaseOn(baseType);
}
///
/// 返回当前类型是否是指定基类的派生类
///
/// 当前类型
/// 要判断的基类型
///
public static bool IsBaseOn(this Type type, Type baseType)
{
if (baseType.IsGenericTypeDefinition)
{
return baseType.IsGenericAssignableFrom(type);
}
return baseType.IsAssignableFrom(type);
}
///
/// 判断当前泛型类型是否可由指定类型的实例填充
///
/// 泛型类型
/// 指定类型
///
public static bool IsGenericAssignableFrom(this Type genericType, Type type)
{
genericType.NotNull(nameof(genericType));
type.NotNull(nameof(type));
if (!genericType.IsGenericType)
{
throw new ArgumentException("该功能只支持泛型类型的调用,非泛型类型可使用 IsAssignableFrom 方法。");
}
List allOthers = new List { type };
if (genericType.IsInterface)
{
allOthers.AddRange(type.GetInterfaces());
}
foreach (var other in allOthers)
{
Type cur = other;
while (cur != null)
{
if (cur.IsGenericType)
{
cur = cur.GetGenericTypeDefinition();
}
if (cur.IsSubclassOf(genericType) || cur == genericType)
{
return true;
}
cur = cur.BaseType;
}
}
return false;
}
///
/// 通过类型转换器获取Nullable类型的基础类型
///
/// 要处理的类型对象
///
public static Type GetUnNullableType(this Type type)
{
if (IsNullableType(type))
{
NullableConverter nullableConverter = new NullableConverter(type);
return nullableConverter.UnderlyingType;
}
return type;
}
/////
///// 转换为Bool类型
/////
/////
/////
//public static bool ObjToBool(this object thisValue)
//{
// bool reval = false;
// if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
// {
// return reval;
// }
// return reval;
//}
///
/// 判断是否IEnumerable、ICollection类型
///
///
///
// public static bool IsEnumerable(this Type type)
// {
// return type.IsArray
// || type.GetInterfaces().Any(x => x == typeof(ICollection) || x == typeof(IEnumerable));
// }
///
/// 从类型成员获取指定Attribute特性
///
/// Attribute特性类型
/// 类型类型成员
/// 是否从继承中查找
/// 存在返回第一个,不存在返回null
public static T GetAttribute(this MemberInfo memberInfo, bool inherit = true) where T : Attribute
{
var attributes = memberInfo.GetCustomAttributes(typeof(T), inherit);
return attributes.FirstOrDefault() as T;
}
///
/// 从类型成员获取指定Attribute特性
///
/// Attribute特性类型
/// 类型类型成员
/// 是否从继承中查找
/// 存在返回第一个,不存在返回null
public static Type GetAttribute(this MemberInfo memberInfo, Type type, bool inherit = true)
{
var attributes = memberInfo.GetCustomAttributes(type, inherit);
return attributes.FirstOrDefault() as Type;
}
///
/// 判断是否实体类型
///
///
///
// public static bool IsEntityType(this Type type)
// {
// type.NotNull(nameof(type));
// return typeof(IEntityWithIdentity<>).IsGenericAssignableFrom(type) && !type.IsAbstract && !type.IsInterface;
// }
///
/// 转换为Guid类型
///
///
///
public static Guid ToGuid(this string str)
{
Guid guid;
if (Guid.TryParse(str, out guid))
{
return guid;
}
else
{
return Guid.Empty;
}
}
///
///
///
///
///
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
public static string ToDescription(this MemberInfo member)
{
DescriptionAttribute desc = member.GetCustomAttribute();
if (!desc.IsNull())
{
return desc.Description;
}
//显示名
DisplayNameAttribute display = member.GetCustomAttribute();
if (!display.IsNull())
{
return display.DisplayName;
}
return member.Name;
}
public static string GetKeySelector(this Type type, string keyName)
{
string[] propertyNames = keyName.Split(".");
return propertyNames.Select(o => type.GetProperty(o)).FirstOrDefault()?.Name;
}
}