using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace NEIntelligentControl2.Models
{
public static class Helper
{
///
/// 获取字符串的配置值
///
///
///
public static string GetConfiguration(this string cfgname)
{
try
{
return System.Configuration.ConfigurationManager.AppSettings[cfgname];
}
catch { }
return "";
}
///
/// 从时间戳获取时间字符串
///
/// 时间戳
/// 时间字符串
public static string GetTimeString(this long l)
{
if (l <= 10000) return "";
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
return startTime.AddMilliseconds(l).ToString("yyyy-MM-dd hh:mm:ss");
}
///
/// 从时间戳获取时间字符串
///
public static string GetTimeSpanString(this long l)
{
if (l < 10000) return "";
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
startTime = startTime.AddMilliseconds(l);
TimeSpan tts = DateTime.Now - startTime;
var ll = (long)tts.TotalMinutes;
ll = ll < 0 ? 0 : ll;
return ll.ToString();
}
///
/// 获取实例中的值
///
public static object GetValue(this object op, string name)
{
string[] names = name.Split('.');
object obj = null;
if (names.Length == 1)
{
obj = op.GetType().GetProperty(name)?.GetValue(op);
}
else
{
var v1 = op.GetType().GetProperty(names[0])?.GetValue(op);
if (v1 != null)
obj = v1.GetType().GetProperty(names[1])?.GetValue(v1);
}
return obj;
}
///
/// 获取枚举的字符
///
public static string GetStringValue(this System.Enum value)
{
string str = "";
var type = value.GetType();
var fi = type.GetField(value.ToString());
var attrs = fi.GetCustomAttributes(typeof(Attributes.StringValue), false) as Attributes.StringValue[];
if(attrs.Length > 0)
{
str = attrs[0].Value;
}
return str;
}
///
/// 从时间获取时间戳值
///
///
///
public static long GetTimeSpan(this DateTime dateTime)
{
DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan time = dateTime.Subtract(dt);
return long.Parse(time.Ticks.ToString().Substring(0, time.Ticks.ToString().Length - 4));
}
///
/// 从时间戳获取时间
///
public static DateTime GetLongDateTime(this long utc)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
startTime = startTime.AddMilliseconds(utc);
return startTime;
}
public static BitmapImage GetBitmapImage(this Bitmap obj)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
obj.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
var bs = ms.GetBuffer();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new System.IO.MemoryStream(bs);
bi.EndInit();
return bi;
}
}
}
}