| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 | using YG;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.Threading.Tasks;using System.ComponentModel;using System.Net.NetworkInformation;namespace System.Text{    public class Notify : System.ComponentModel.INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void NotifyProper(string name)        {            if (PropertyChanged != null)            {                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));            }        }    }    public static class CommonAtrr    {        public static void ShowDialog(this System.Windows.Forms.Form fm, string msg, bool isthread = true)        {            if (isthread)            {                Task.Factory.StartNew(() =>                {                    System.Windows.Forms.MessageBox.Show(msg, "信息提示", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Asterisk);                });            }            else            {                System.Windows.Forms.MessageBox.Show(msg, "信息提示", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Asterisk);            }        }        /// <summary>        /// 判断text里边是否为空,如果为空那么弹出提示信息        /// </summary>        /// <param name="text"></param>        /// <param name="showmsg"></param>        public static bool Text_Check_value_Empty(this System.Windows.Forms.TextBox text, string showmsg)        {            if (string.IsNullOrEmpty(text.Text))            {                Task.Factory.StartNew(() => { System.Windows.Forms.MessageBox.Show(showmsg, "信息提示", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Asterisk); });                return false;            }            return true;        }        public static byte[] ReturnBtyesWtitString(this string value, int byteleng = 30)        {            byte[] bt = new byte[byteleng];            byte[] item = Encoding.ASCII.GetBytes(value.ToClone());            Array.Copy(item, bt, item.Length);            return bt;        }        public static bool Ping(this string ip)        {            Ping ping = new Ping();            PingReply pingReply = ping.Send(ip.ToClone());            if (pingReply.Status.Equals(IPStatus.Success))            {                return true;            }            return false;        }        /// <summary>        /// 判断当前路径是否存在, 如果不存在就创建,        /// </summary>        /// <param name="path"></param>        /// <returns>如果为false则表示当前路径不存在,true则表示当前路径存在</returns>        public static bool PathIsExist(this string path)        {            try            {                if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path.ToClone())))                {                    System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));                }                if (!File.Exists(path))                {                    File.Create(path).Close();                    return false;                }            }            catch (Exception ex)            {                YG.Log.Instance.WriteLogAdd($"87-->{ex.Message}");                return false;            }            return true;        }        /// <summary>        /// 判断text里边是否为空,并且文本的长度是否小于当前参数,如果小于那么弹出提示信息        /// </summary>        /// <param name="text"></param>        /// <param name="textleng"></param>        /// <param name="showmsg"></param>        public static bool Text_Check_valueLeng_Empty(this System.Windows.Forms.TextBox text, int textleng, string showmsg)        {            if (string.IsNullOrEmpty(text.Text) && text.Text.Length < textleng)            {                Task.Factory.StartNew(() => { System.Windows.Forms.MessageBox.Show(showmsg); });                return false;            }            return true;        }        /// <summary>        /// 把字符转换为int        /// </summary>        /// <param name="value"></param>        /// <returns></returns>        public static int StringToInt(this string value)        {            try { return Convert.ToInt32(value.ToClone()); }            catch            {                Log.Instance.WriteLogAdd(value + "在进行类型转换StringToInt时出现异常", null);                return 0;            }        }        /// <summary>        /// 把字符转换为int        /// </summary>        /// <param name="value"></param>        /// <returns></returns>        public static bool StringToBool(this string value)        {            try { return Convert.ToBoolean(value.StringToInt()); }            catch            {                Log.Instance.WriteLogAdd(value + "在进行类型转换StringToBool时出现异常", null);                return false;            }        }        /// <summary>        /// 拷贝        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="List">The list.</param>        /// <returns>List{``0}.</returns>        public static System.ComponentModel.BindingList<T> Clone<T>(this System.ComponentModel.BindingList<T> List)        {            using (Stream objectStream = new MemoryStream())            {                IFormatter formatter = new BinaryFormatter();                formatter.Serialize(objectStream, List);                objectStream.Seek(0, SeekOrigin.Begin);                return formatter.Deserialize(objectStream) as System.ComponentModel.BindingList<T>;            }        }        public static List<T> Clone<T>(this List<T> List)        {            using (Stream objectStream = new MemoryStream())            {                IFormatter formatter = new BinaryFormatter();                formatter.Serialize(objectStream, List);                objectStream.Seek(0, SeekOrigin.Begin);                return formatter.Deserialize(objectStream) as List<T>;            }        }        public static float StringToFloat(this string value)        {            try { return Convert.ToSingle(value.ToClone()); }            catch            {                Log.Instance.WriteLogAdd(value + "在进行类型转换StringToFloat时出现异常", null);                return 0;            }        }        /// <summary>        /// 把字符转换为double类型         /// </summary>        /// <param name="value"></param>        /// <returns></returns>        public static double StringToDouble(this string value)        {            try { return Convert.ToDouble(value.ToClone()); }            catch            {                Log.Instance.WriteLogAdd(value + "在进行类型转换StringToDouble时出现异常");                return 0;            }        }        /// <summary>        /// 把噢bject类型转换为int类型        /// </summary>        /// <param name="ob"></param>        /// <returns></returns>        public static int ObjectToInt(this object ob)        {            return ob.ToString().StringToInt();        }        public static string GuidLeng(this Guid guid,int leng=30)        {            return guid.ToString().Substring(0, leng);        }        public static int BoolToInt(this bool bl)        {            if (bl)            {                return 1;            }            return 0;        }        public static short ObjectToShort(this object ob)        {            return short.Parse(ob.ToString());        }                public static byte ObjectToByte(this object ob)        {            return byte.Parse(ob.ToString());        }        public static string DateTimeToString(this DateTime dt)        {            return dt.ToString("yyyy-MM-dd HH:mm:ss");        }        /// <summary>        /// 把噢bject类型转换为double类型        /// </summary>        /// <param name="ob"></param>        /// <returns></returns>        public static double ObjectToDouble(this object ob)        {            return ob.ToString().StringToDouble();        }        public static string ObjectToString(this object ob)        {            try { if (ob != null) return ob.ToString(); else Log.Instance.WriteLogAdd("转换object为string类型时出现哦bject为空的情况默认返回空字符串"); return ""; }            catch(Exception ex) { YG.Log.Instance.WriteLogAdd($"216-->{ex.Message}"); return ""; }        }        public static void DataGridViewColor(this System.Windows.Forms.DataGridView dv)        {            dv.RowsDefaultCellStyle.BackColor = Drawing.Color.AliceBlue;            dv.AlternatingRowsDefaultCellStyle.BackColor = Drawing.Color.Aquamarine;        }        /// <summary>        /// 判断当前的FirstOrDefaul是否为空,如果为空,那么不执行参数函数        /// </summary>        /// <typeparam name="TSource"></typeparam>        /// <param name="source"></param>        /// <param name="action"></param>        public static bool FirstOrDefaultYG<TSource>(this TSource source, Action<TSource> action)        {            if (source != null)            {                try                {                    action(source);                    return true;                }                catch                {                    Log.Instance.WriteLogAdd("执行FirstOrDefaultYG函数时出现异常情况");                    return false;                }            }            return false;        }        public static bool Foreach<TSource>(this IEnumerable<TSource> source, Action<TSource> action)        {            if (source != null)            {                try                {                    foreach (var c in source)                    {                        action(c);                    }                    return true;                }                catch                {                    Log.Instance.WriteLogAdd("执行Foreach函数时出现异常情况");                    return false;                }            }            return false;        }        /// <summary>        /// 把对象转换为字符串        /// </summary>        /// <param name="ob"></param>        /// <returns></returns>        public static string Json_SerializeObject(this object ob)        {            return Newtonsoft.Json.JsonConvert.SerializeObject(ob);        }        /// <summary>        /// 把字符串转换为对象        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="value"></param>        /// <returns></returns>        public static T Json_DeserializeObject<T>(this string value)        {            try            {                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(value);            }            catch            {                Log.Instance.WriteLogAdd(string.Format("转换类型时出现异常{0}-->{1}", typeof(T), value));                return default(T);            }        }        /// <summary>        /// 如果字符串为空, 那么返回true        /// </summary>        /// <param name="msg"></param>        /// <returns></returns>        public static bool StringIsEmpty(this string msg)        {            if (string.IsNullOrEmpty(msg))            {                return true;            }            return false;        }        /// <summary>        /// 通用summary        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="t"></param>        /// <param name="sw"></param>        /// <param name="ishead"></param>        /// <returns></returns>        private static bool ObjectWriteCVS<T>(this T t, StreamWriter sw, bool ishead)        {            try            {                System.Reflection.PropertyInfo[] minfo = t.GetType().GetProperties();                if (minfo.Length > 0)                {                    foreach (System.Reflection.PropertyInfo mi in minfo)                    {                        if (mi.PropertyType.IsGenericType)                        {                            object ob = mi.GetValue(t, null);                            int count = (int)ob.GetType().GetProperty("Count").GetValue(ob);//.GetValue(t);                            for (int i = 0; i < count; i++)                            {                                object item = ob.GetType().GetProperty("Item").GetValue(ob, new object[] { i });                                if (item.GetType().GetProperties().Where(m => m.Name.Equals("Item_type") && m.GetValue(item).Equals("Elect")).Count() > 0)                                {                                    break;                                }                                item.ObjectWriteCVS(sw, ishead);                            }                        }                        else                        {                            //if (mi.Name.Equals("Item_type") && mi.GetValue(t).Equals("Elect"))                            //{                            //    break;                            //}                            object[] atrti = mi.GetCustomAttributes(false);                            if (atrti != null && atrti.Count() > 0)                            {                                YGAttribute YGa = mi.GetCustomAttributes(false)[0] as YGAttribute;                                if (YGa != null && YGa.IsWrite)                                {                                    if (ishead)                                    {                                        sw.Write(mi.Name);                                        sw.Write("\t");                                    }                                    else                                    {                                        sw.Write(mi.GetValue(t).ToString().Replace("\r", " ").Replace("\n", " ").Replace("\t", " "));                                        sw.Write("\t");                                    }                                    //   sw.Write("\t");                                }                            }                        }                    }                }                return true;            }            catch (Exception ex)            {                YG.Log.Instance.WriteLogAdd($"377-->{ex.Message}");                return false;            }        }        /// <summary>        /// 把对象写入到cvs表格里便        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="t"></param>        /// <param name="path"></param>        public static bool ObjectWriteCVS<T>(this T t, string path)        {            try            {                System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);                StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());                System.Reflection.PropertyInfo[] minfo = t.GetType().GetProperties();                if (minfo.Length > 0)                {                    foreach (System.Reflection.PropertyInfo mi in minfo)                    {                        if (mi.PropertyType.IsGenericType)                        {                            object ob = mi.GetValue(t, null);                            int count = (int)ob.GetType().GetProperty("Count").GetValue(ob);//.GetValue(t);                            for (int i = 0; i < count; i++)                            {                                object item = ob.GetType().GetProperty("Item").GetValue(ob, new object[] { i });                                foreach (var pro in item.GetType().GetProperties())                                {                                    object[] atrti = pro.GetCustomAttributes(false);                                    if (atrti != null && atrti.Count() > 0)                                    {                                        YGAttribute YGa = pro.GetCustomAttributes(false)[0] as YGAttribute;                                        if (YGa != null && YGa.IsWrite)                                        {                                            sw.Write(pro.GetValue(item));                                            sw.Write("\t");                                        }                                    }                                }                            }                        }                    }                }                sw.WriteLine("");                sw.Flush();                sw.Close();                return true;            }            catch (Exception ex)            {                YG.Log.Instance.WriteLogAdd(ex.Message);                return false;            }        }    }    /// <summary>    /// 是否写入CVS    /// </summary>    public class YGAttribute : Attribute    {        /// <summary>        /// 是否写入CVS        /// </summary>        public bool IsWrite { get; set; } = true;    }}
 |