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());
}
///
/// 判断当前的FirstOrDefaul是否为空,如果为空,那么不执行参数函数
///
///
///
///
public static bool FirstOrDefaultYG(this TSource source, Action action)
{
if (source != null)
{
try
{
action(source);
return true;
}
catch
{
Log.Instance.WriteLogAdd("执行FirstOrDefaultYG函数时出现异常情况");
return false;
}
}
return false;
}
public static bool Foreach(this IEnumerable source, Action action)
{
if (source != null)
{
try
{
foreach (var c in source)
{
action(c);
}
return true;
}
catch
{
Log.Instance.WriteLogAdd("执行Foreach函数时出现异常情况");
return false;
}
}
return false;
}
///
/// 把对象转换为字符串
///
///
///
public static string Json_SerializeObject(this object ob)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(ob);
}
///
/// 把字符串转换为对象
///
///
///
///
public static T Json_DeserializeObject(this string value)
{
try
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(value);
}
catch
{
Log.Instance.WriteLogAdd(string.Format("转换类型时出现异常{0}-->{1}", typeof(T), value));
return default(T);
}
}
///
/// 如果字符串为空, 那么返回true
///
///
///
public static bool StringIsEmpty(this string msg)
{
if (string.IsNullOrEmpty(msg))
{
return true;
}
return false;
}
///
/// 通用summary
///
///
///
///
///
///
private static bool ObjectWriteCVS(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;
}
}
///
/// 把对象写入到cvs表格里便
///
///
///
///
public static bool ObjectWriteCVS(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;
}
}
}
///
/// 是否写入CVS
///
public class YGAttribute : Attribute
{
///
/// 是否写入CVS
///
public bool IsWrite { get; set; } = true;
}
}