• .NET 实用扩展方法


    .NET 实用扩展方法(持续更新...)

    1. 字符串转换为可空数值类型(int, long, float...类似)

        /// <summary>
        /// 将字符串转换成32位整数,转换失败返回null
        /// </summary>
        /// <param name="str">转换的字符串</param>
        /// <returns>转换之后的整数,或null</returns>
        public static int? TryParseToInt32(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return null;
            var result = 0;
            if (int.TryParse(str, out result))
                return result;
            else
                return null;
        }
    

    2. 去除子字符串

        /// <summary>
        /// 去除子字符串
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="substring">要去除的字符串</param>
        /// <returns>去除子字符串之后的结果</returns>
        public static string DeSubstring(this string str, string substring)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(substring) || !str.Contains(substring))
            {
                return str;
            }
    
            return Regex.Replace(str, Regex.Escape(substring), string.Empty);
        }
    
        /// <summary>
        /// 去除子字符串
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="substrings">要去除的子字符串</param>
        /// <returns>去除子字符串之后的结果</returns>
        public static string DeSubstring(this string str, params string[] substrings)
        {
            if (string.IsNullOrEmpty(str))
                return str;
            if (substrings == null)
                return str;
            var newStr = str;
            foreach (var item in substrings)
            {
                newStr = newStr.DeSubstring(item);
            }
            return newStr;
        }
    

    3. 获取子序列

        /// <summary>
        /// 获取子序列
        /// </summary>
        /// <typeparam name="T">序列中元素类型</typeparam>
        /// <param name="source">源数据</param>
        /// <param name="startIndex">开始索引(返回时包括)</param>
        /// <param name="endIndex">结束索引(返回时包括)</param>
        /// <returns>子序列</returns>
        public static IEnumerable<T> SubEnumerable<T>(this IEnumerable<T> source, int startIndex, int endIndex)
        {
            if (source == null)
                yield return default(T);
            var length = source.Count();
            if (startIndex < 0 || endIndex < startIndex || startIndex >= length || endIndex >= length)
                throw new ArgumentOutOfRangeException();
    
            var index = -1;
            foreach (var item in source)
            {
                index++;
                if (index < startIndex)
                    continue;
                if (index > endIndex)
                    yield break;
                yield return item;
            }
        }
    

    4. 通过指定键对序列去重, 不必实现IEqualityComparer接口

        /// <summary>
        /// 通过对指定的值进行比较返回序列中的非重复元素。
        /// </summary>
        /// <typeparam name="T">序列中元素类型</typeparam>
        /// <typeparam name="TResult">指定的比较属性类型</typeparam>
        /// <param name="source">源数据</param>
        /// <param name="selector">应用于每个元素的转换函数</param>
        /// <returns>一个包含源序列中的按指定属性非重复元素</returns>
        public static IEnumerable<T> Distinct<T, TResult>(this IEnumerable<T> source, Func<T, TResult> selector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));
            var set = new HashSet<TResult>();
            foreach (var item in source)
            {
                var result = selector(item);
                if (set.Add(result))
                {
                    yield return item;
                }
            }
        }
    

    5. 获取序列中重复的元素序列, 原理和去重类似

        /// <summary>
        /// 通过对指定的值进行比较返回序列中重复的元素
        /// </summary>
        /// <typeparam name="T">序列中的数据类型</typeparam>
        /// <typeparam name="TResult">指定的比较属性类型</typeparam>
        /// <param name="source">源数据</param>
        /// <param name="selector">应用于每个元素的转换函数</param>
        /// <returns>一个包含源序列中的按指定元素的重复元素</returns>
        public static IEnumerable<T> Identical<T>(this IEnumerable<T> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            var setT = new HashSet<T>();
            foreach (var item in source)
            {
                if (!setT.Add(item))
                {
                    yield return item;
                }
            }
        }
    
        /// <summary>
        /// 通过对指定的值进行比较返回序列中重复的元素
        /// </summary>
        /// <typeparam name="T">序列中的数据类型</typeparam>
        /// <typeparam name="TResult">指定的比较属性类型</typeparam>
        /// <param name="source">源数据</param>
        /// <param name="selector">应用于每个元素的转换函数</param>
        /// <returns>一个包含源序列中的按指定元素的重复元素</returns>
        public static IEnumerable<T> Identical<T, TResult>(this IEnumerable<T> source, Func<T, TResult> selector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));
            var setTResult = new HashSet<TResult>();
            foreach (var item in source)
            {
                var result = selector(item);
                if (!setTResult.Add(result))
                {
                    yield return item;
                }
            }
        }
    

    6. 使用注解对实体进行验证, 可用在除ASP.NET MVC之外的任一框架中(MVC已内置), 需引用System.ComponentModel.DataAnnotations类库及命名空间

        /// <summary>
        /// 通过使用验证上下文和验证结果结合, 确定指定的对象是否有效
        /// </summary>
        /// <param name="instance">要验证的对象</param>
        /// <param name="validationResults">用于包含每个失败的验证的集合</param>
        /// <returns>如果对象有效,为 true, 否则为 false</returns>
        public static bool TryValidate(this object instance, ICollection<ValidationResult> validationResults = null)
        {
            var context = new ValidationContext(instance);
            return Validator.TryValidateObject(instance, context, validationResults, true);
        }
    
        /// <summary>
        /// 通过使用验证上下文和验证结果结合, 确定指定的方法参数是否有效
        /// </summary>
        /// <param name="instance">要验证的方法所属对象</param>
        /// <param name="methodName">类中要验证的方法名称</param>
        /// <param name="values">方法参数的值</param>
        /// <param name="validationResults">用于包含每个失败的验证的集合</param>
        /// <returns>如果参数有效,为 true, 否则为 false</returns>
        public static bool TryValidate(this object instance, string methodName, object[] values, ICollection<ValidationResult> validationResults = null)
        {
            var typeOfInstance = instance.GetType();
            var methodInfo = typeOfInstance.GetMethod(methodName, values.Select(p => p?.GetType() ?? typeof(object)).ToArray());
            if (methodInfo == null)
            {
                //查找可能存在的泛型方法
                methodInfo = typeOfInstance
                            .GetMethods()
                            .FirstOrDefault(p => p.Name == methodName
                                              && p.IsGenericMethod
                                              && p.GetParameters().Length == values.Length);
            }
            if (methodInfo == null)
                throw new ArgumentException("找不到指定的方法,请检查方法名或者参数数组是否存在匹配");
            var paramInfos = methodInfo.GetParameters();
            bool isValid = true;
            for (var i = 0; i < values.Length; i++)
            {
                var value = values[i];
                var paramInfo = paramInfos.ElementAt(i);
                var context = new ValidationContext(paramInfo);
                context.DisplayName = paramInfo.Name;
                var attrs = paramInfo.GetCustomAttributes<ValidationAttribute>();
                isValid = isValid & Validator.TryValidateValue(value, context, validationResults, attrs);
            }
            return isValid;
        }
    
        /// <summary>
        /// 通过使用验证上下文和验证结果结合, 确定指定的方法参数是否有效
        /// </summary>
        /// <param name="instance">要验证的方法所属对象</param>
        /// <param name="methodName">类中要验证的方法名称</param>
        /// <param name="values">方法参数值</param>
        public static void Validate(this object instance, string methodName, object[] values)
        {
            var typeOfInstance = instance.GetType();
            var methodInfo = typeOfInstance.GetMethod(methodName, values.Select(p => p?.GetType() ?? typeof(object)).ToArray());
            if (methodInfo == null)
            {
                //查找可能存在的泛型方法
                methodInfo = typeOfInstance
                            .GetMethods()
                            .FirstOrDefault(p => p.Name == methodName
                                              && p.IsGenericMethod
                                              && p.GetParameters().Length == values.Length);
            }
            if (methodInfo == null)
                throw new ArgumentException("找不到指定的方法,请检查方法名或者参数数组是否存在匹配");
            var paramInfos = methodInfo.GetParameters();
            for (var i = 0; i < values.Length; i++)
            {
                var value = values[i];
                var paramInfo = paramInfos.ElementAt(i);
                var context = new ValidationContext(paramInfo);
                Validator.ValidateValue(value, context, paramInfo.GetCustomAttributes<ValidationAttribute>());
            }
        }
    

    验证的目标实体:

        public class User
        {
            [Required]          //表示此字段不能为null或空字符串
            [StringLength(3)]   //表示此字段能接收的最大字符串长度
            public string Name { get; set; }
    
            [Range(0, 120)]     //表示数据字段的最小最大范围
            public int Age { get; set; }
        }
    

    实体验证:

        User user = new User() { Name = "A,ning", Age = 140 };
    
        ICollection<ValidationResult> errors = new List<ValidationResult>();
    
        //如果不关注验证结果, 可不传入 errors 参数, 验证失败将只返回false
        if (user.TryValidate(errors))
        {
            Console.WriteLine("验证通过");
        }
        else
        {
            Console.WriteLine("验证失败");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(string.Join(Environment.NewLine, errors.Select(p => p.ErrorMessage)));
        }
    

    方法参数验证(支持泛型方法):

    public void SayHello<T>([Required][StringLength(3)] T name, [Range(0, 120)] int age)
    {
        var errors = new List<ValidationResult>();
        if (this.TryValidate(nameof(SayHello), new object[] { name, age }, errors))
        {
            Console.WriteLine("验证通过");
        }
        else
        {
            Console.WriteLine("验证失败");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(string.Join(Environment.NewLine, errors.Select(p => p.ErrorMessage)));
        }
    }
    

    7. 判断Type是否为.NET Framework内置类型

        /// <summary>
        /// 判断<see cref="Type"/>是否为用户自定义类型
        /// </summary>
        /// <param name="type">type</param>
        /// <returns>如果为 .NET 内置类型返回false,否则返回true</returns>
        public static bool IsCustomType(this Type type)
        {
            return (type != typeof(object) && Type.GetTypeCode(type) == TypeCode.Object);
        }
  • 相关阅读:
    leetcode131分割回文串
    leetcode315 计算右侧小于当前元素的个数
    Tensorflow写代码流程&反向传播
    vue脚手架的搭建
    Vue 脱坑记
    简历中的工作经历要怎么写?
    如何制作高水平简历?
    window.location.hash的知识点
    前端面试题小集
    前端面试题
  • 原文地址:https://www.cnblogs.com/aning2015/p/7660160.html
Copyright © 2020-2023  润新知