123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- namespace IMCS.CCS.Utils
- {
- 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 short ObjectToShort(this object ob)
- {
- return short.Parse(ob.ToString());
- }
- public static byte ObjectToByte(this object ob)
- {
- return byte.Parse(ob.ToString());
- }
-
- /// <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)
- {
- 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)
- {
- return false;
- }
- }
- }
- /// <summary>
- /// 是否写入CVS
- /// </summary>
- public class YGAttribute : Attribute
- {
- /// <summary>
- /// 是否写入CVS
- /// </summary>
- public bool IsWrite { get; set; } = true;
- }
- }
|