Extends.cs 6.98 KB
using RCS.Model;
using RCS.Model.Comm;
using RCS.Model.Entity;
using System.ComponentModel;
using static RCS.Model.Comm.EnumMsg;


namespace RCS.WinClient.Common
{
    public static class Extends
    {
        /// <summary>
        /// 获取点的Point属性
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public static BllResult<Base_Point> Point(this string barcode)
        {
            var point = App.PointList.Find(a => a.Barcode == barcode);
            if (point == null)
            {
                return BllResult<Base_Point>.Error();
            }
            return BllResult<Base_Point>.Success(point);
        }

        /// <summary>
        /// 获取区间值
        /// </summary>
        /// <param name="value">被比较的X轴或是Y轴</param>
        /// <param name="doublieX">当前点的Y轴</param>
        /// <param name="doubleY">当前点的X轴</param>
        /// <param name="flag">1比较X轴,2比较Y轴</param>
        /// <returns></returns>
        public static bool intBetween(this int value, double doublieX, double doubleY, int flag)
        {
            if (flag == 1)
            {
                if (doublieX > -2600 && doublieX < -2500)
                {
                    return value < doublieX + 5 && value > doublieX - 5;
                }
                else if (doublieX > -10200 && doublieX < -8729 && doubleY < 500 && doubleY > 300)
                {
                    return value < doublieX + 6 && value > doublieX - 6;
                }
                else if (doublieX > 1800 && doublieX < 1970)
                {
                    return value < doublieX + 10 && value > doublieX - 10;
                }
                return value < doublieX + 25 && value > doublieX - 25;
            }
            return value < doubleY + 25 && value > doubleY - 25;
        }

        /// <summary>
        /// 获取角度区间值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public static bool DLAgvOriBetween(this float value, float num)
        {
            return value < num + 60 && value > num - 60;
        }

        /// <summary>
        /// 获取枚举成员的值(this是扩展方法的标志)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int GetIndexInt(this Enum obj)
        {
            return Convert.ToInt32(obj);
        }

        /// <summary>
        /// 获取枚举成员的值(this是扩展方法的标志)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetIndexString(this Enum obj)
        {
            return Convert.ToInt32(obj).ToString();
        }

        public static T ToEnum<T>(this string obj) where T : struct
        {
            if (string.IsNullOrEmpty(obj))
            {
                return default(T);
            }
            try
            {
                return (T)Enum.Parse(typeof(T), obj, true);
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        /// <summary>
        /// 获取指定枚举成员的描述
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToDescriptionString(this Enum obj)
        {
            var attribs = (DescriptionAttribute[])obj.GetType().GetField(obj.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attribs.Length > 0 ? attribs[0].Description : obj.ToString();
        }

        /// <summary>
        /// 根据枚举值,获取指定枚举类的成员描述
        /// </summary>
        /// <param name="type"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetDescriptionString(this Type type, int? id)
        {
            var values = from Enum e in Enum.GetValues(type)
                         select new { id = e.GetIndexInt(), name = e.ToDescriptionString() };

            if (!id.HasValue) id = 0;

            try
            {
                return values.ToList().Find(c => c.id == id).name;
            }
            catch
            {
                return "";
            }
        }

        public static string GetDateString(this DateTime? dateTime)
        {
            if (dateTime == null)
            {
                return "null";
            }
            else
            {
                return dateTime.ToString();
            }
        }

        public static DateTime? GetDateTime(this object str)
        {
            if (string.IsNullOrWhiteSpace(str.ToString()))
            {
                return null;
            }
            else
            {
                return DateTime.Parse(str.ToString());
            }
        }

        public static List<T> EnumToList<T>() where T : Enum
        {
            return Enum.GetValues(typeof(T)).Cast<T>().ToList();
        }

        /// <summary>
        /// 根据索引获取列表数据,当列表为空时,返回默认值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static T? Get<T>(this IList<T> list, int index)
        {
            T? item = ((list?.Count ?? 0) - 1 < index || list!.Count < index) ? default : list![index];
            return item;
        }


        public static bool IsNullOrEmpty(this string? str)
        {

            return string.IsNullOrEmpty(str);
        }

        public static bool In<T>(this T obj, params T[] arg)
        {

            for (int i = 0; i < arg.Length; i++)
            {
                if (arg[i] == null)
                {
                    if (obj == null)
                        return true;
                    continue;
                }
                else if (arg[i]!.Equals(obj))
                {
                    return true;
                }
            }

            return false;
        }


        public static bool IntBetween(this int value, double value2, int error = 25)
        {
            return Math.Abs(value2 - value) < error;
        }
    }

    public class NullFormat : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type service)
        {
            if (service == typeof(ICustomFormatter))
            {
                return this;
            }
            else
            {
                return null;
            }
        }

        public string Format(string format, object arg, IFormatProvider provider)
        {
            if (arg == null)
            {
                return "null";
            }
            IFormattable formattable = arg as IFormattable;
            if (formattable != null)
            {
                return formattable.ToString(format, provider);
            }
            return arg.ToString();
        }
    }

}