全部以图片格式存储:
场景
现在公司的测试环境一些文本日志不让接触,提供一个网络http服务器让人直接访问,这文件大时,一般10MB一个文件,不在同一局域网,网速限制200K,要等很久,访问很慢。
.Net代码请求文本文件最新内容(类似于tail文件)
安装、简易教程:http://www.runoob.com/nodejs/nodejs-tutorial.html
官方文档:http://nodejs.cn/api/
后面的三个方法是加的,前面的经过压缩的是插件原来的内容。
三种单例模式写法:
public class Singleton { private static Singleton instance; private static readonly object syncRoot = new object(); private Singleton() { } public static Singleton GetInstance() { if (instance == null)//Double-Check Locking 双重锁定,比直接lock性能更好,最完美的模式(懒汉式单例) { lock (syncRoot) { if (instance == null) { instance = new Singleton(); } } } return instance; } } public class Singleton1 { private static Singleton1 instance; private static readonly object syncRoot = new object(); private Singleton1() { } public static Singleton1 GetInstance() { lock (syncRoot)//每次调用GetInstance都需要lock,影响性能(懒汉式单例) { if (instance == null) { instance = new Singleton1(); } } return instance; } } public class Singleton2 { private static Singleton2 instance=new Singleton2();//静态初始化方式(饿汉式单例),类一加载就实例化对象,一般这样用也没问题,缺点:提前占用系统资源 private Singleton2() { } public static Singleton2 GetInstance() { return instance; } }
一直以为Math.Round就是四舍五入,谁知道没加参数,得到的结果就是有问题
测试代码:
void Main() { string.Format("Round {0} = {1}",2.4M,Math.Round(2.4M,0)).Dump(); string.Format("Round {0} = {1}",2.5M,Math.Round(2.5M,0)).Dump(); string.Format("Round {0} = {1}",2.6M,Math.Round(2.6M,0)).Dump(); string.Format("Round {0} = {1}",3.4M,Math.Round(3.4M,0)).Dump(); string.Format("Round {0} = {1}",3.5M,Math.Round(3.5M,0)).Dump(); string.Format("Round {0} = {1}",3.6M,Math.Round(3.6M,0)).Dump(); }
输出内容:
js遍历类、json对象属性、值,
我们在页面,经常会接收后面或json返回的数据,要一个一个赋值,遍历json属性,可以方便给界面的控件赋值。
我们知道程序中使用异步、多线程会提高程序的响应速度,但也不能无限使用多线程,这在高峰会造成系统cpu上升,系统卡顿,这就需要我们自己来控制开启的线程数,不多说看代码。
private static int threadCountByOrderId = 0; private static int maxThreadCountByOrderId = 30; public bool dealorder(int OrderId) { var threadNumber = Interlocked.Exchange(ref threadCountByOrderId, threadCountByOrderId); if (threadCountByOrderId > maxThreadCountByOrderId) { return OrderChangePushToBigDataImpService.PushOrderToBigData(OrderId, Logger); } else { Task.Factory.StartNew(() => { Interlocked.Increment(ref threadCountByOrderId); try { using (var context = MefInjectionProvider.CreateContext()) { var NewOrderChangePushToBigDataImpService = context.Value.GetExport<IOrderChangePushToBigDataImpService>(); NewOrderChangePushToBigDataImpService.PushOrderToBigData(OrderId, Logger); } } finally { Interlocked.Decrement(ref threadCountByOrderId); } }); return true; } }
这样就能控制线程数据了
直接上代码,传入新旧两个Model类
字段说明,要加display标签:
代码如下:
public static class EntityExtension { public static HashSet<Type> PrimitiveTypes = null; static EntityExtension() { PrimitiveTypes = new HashSet<Type>() { typeof(String), typeof(Byte[]), typeof(Byte), typeof(Int16), typeof(Int32), typeof(Int64), typeof(Single), typeof(Double), typeof(Decimal), typeof(DateTime), typeof(Guid), typeof(Boolean), typeof(TimeSpan), typeof(Byte?), typeof(Int16?), typeof(Int32?), typeof(Int64?), typeof(Single?), typeof(Double?), typeof(Decimal?), typeof(DateTime?), typeof(Guid?), typeof(Boolean?), typeof(TimeSpan?) }; } public static string GetChangedFields<T>(this T newEntity, T oldEntity) where T : class { StringBuilder updatedFields = new StringBuilder(); Type entityType = typeof(T); PropertyInfo[] properties = entityType.GetProperties().Where(o => o.CanWrite && PrimitiveTypes.Contains(o.PropertyType) && !o.GetCustomAttributes(false).OfType<NotMappedAttribute>().Any()).ToArray(); foreach (var p in properties) { if (p.Name == "ModifiedDate" || p.Name == "ModifiedByName" || p.Name == "ModifiedById") continue; object oldValue = p.GetValue(oldEntity, null); object newValue = p.GetValue(newEntity, null); if ((oldValue == null && newValue == null)) { continue; } else if (oldValue == null && newValue != null || oldValue != null && newValue == null || !Eq(p.PropertyType, oldValue, newValue)) { string fieldName; var display = p.GetCustomAttribute<DisplayAttribute>(false); fieldName = display != null ? display.Name : p.Name; updatedFields.AppendFormat("{0}:{1}->{2}; ", fieldName, oldValue ?? "NULL", newValue ?? "NULL"); } } return updatedFields.ToString(); } private static bool Eq(Type propertyType, object oldValue, object newValue) { if (propertyType == typeof(Decimal) || propertyType == typeof(Decimal?)) { return decimal.Parse(oldValue.ToString()) == decimal.Parse(newValue.ToString()); } else { return string.Equals(oldValue.ToString(), newValue.ToString()); } } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="newEntity"></param> /// <param name="oldEntity"></param> /// <returns></returns> public static List<ValueModifiedModel> GetChangedFieldsExtent<T>(this T newEntity, T oldEntity) where T : class { List<ValueModifiedModel> valueModifiedList = new List<ValueModifiedModel>(); Type entityType = typeof(T); PropertyInfo[] properties = entityType.GetProperties().Where(o => o.CanWrite && PrimitiveTypes.Contains(o.PropertyType) && !o.GetCustomAttributes(false).OfType<NotMappedAttribute>().Any()).ToArray(); foreach (var p in properties) { if (ignoreColumnList.Contains(p.Name)) continue; object oldValue = p.GetValue(oldEntity, null); object newValue = p.GetValue(newEntity, null); if ((oldValue == null && newValue == null)) { continue; } else if (oldValue == null && newValue != null || oldValue != null && newValue == null || !Eq(p.PropertyType, oldValue, newValue)) { valueModifiedList.Add(new ValueModifiedModel() { ColumnName = p.Name, ColumnChineseName = p.GetPropertyDisplayName(), ChangeBefore = oldValue ?? "NULL", ChangeAfter = newValue ?? "NULL" } ); } } return valueModifiedList; } /// <summary> /// / /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pTargetObjSrc"></param> /// <param name="pTargetObjDest"></param> public static void EntityToEntity<T>(T pTargetObjSrc, T pTargetObjDest) where T : class { try { foreach (var mItem in typeof(T).GetProperties()) { mItem.SetValue(pTargetObjDest, mItem.GetValue(pTargetObjSrc, new object[] { }), null); } } catch (NullReferenceException NullEx) { throw NullEx; } catch (Exception Ex) { throw Ex; } } /// <summary> /// 忽略的列名列表 /// </summary> public static List<string> ignoreColumnList = new List<string>() { }; }