using System; using System.Collections; using System.Collections.Concurrent; using System.Linq.Expressions; namespace Masuit.Tools { /// /// 工具类 /// internal static class MapperTools { private static readonly Type _typeString = typeof(string); private static readonly Type _typeIEnumerable = typeof(IEnumerable); private static readonly ConcurrentDictionary> _ctors = new ConcurrentDictionary>(); /// /// 判断是否是string以外的引用类型 /// /// True:是string以外的引用类型,False:不是string以外的引用类型 public static bool IsRefTypeExceptString(Type type) => !type.IsValueType && type != _typeString; /// /// 判断是否是string以外的可遍历类型 /// /// True:是string以外的可遍历类型,False:不是string以外的可遍历类型 public static bool IsIEnumerableExceptString(Type type) => _typeIEnumerable.IsAssignableFrom(type) && type != _typeString; /// /// 创建指定类型实例 /// /// 类型信息 /// 指定类型的实例 public static object CreateNewInstance(Type type) => _ctors.GetOrAdd(type, t => Expression.Lambda>(Expression.New(t)).Compile())(); } }