//DataRow转实体 private static T CreateItem <T>(DataRow row) { T obj = default(T); if (row == null) { return(obj); } obj = Activator.CreateInstance <T>(); foreach (DataColumn column in row.Table.Columns) { var prop = obj.GetType().GetProperty(column.ColumnName); try { var value = row[column.ColumnName]; if (prop != null) { prop.SetValue(obj, value, null); } } catch (Exception ex) { TyLog.WriteError($"{column.ColumnName}转换失败:{ex}"); } } return(obj); }
/// <summary> /// 删除注册表实现 解除开机自动启动 /// </summary> /// <param name="appName">日志内容</param> public static void UnAutoStart(string appName) { try { var rKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); rKey?.DeleteValue(appName, true); } catch (Exception e) { TyLog.WriteError(e); } }
/// <summary> /// 将JSON数据转化为对应的类型 /// </summary> /// <typeparam name="T">要转换的类型</typeparam> /// <param name="jsonStr">json字符串</param> /// <returns>转换后的对象</returns> public static T JsonToObj <T>(string jsonStr) { try { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr))) { return((T) new DataContractJsonSerializer(typeof(T)).ReadObject(ms)); } } catch (Exception ex) { TyLog.WriteError(ex); return(default(T)); } }