c# 常用 Common
/// <summary>/// md5加密字符串/// </summary>/// <param name="message"></param>/// <returns></returns>public static String ToMD5(this String message){MD5 md5 = new MD5CryptoServiceProvider();byte[] result = Encoding.UTF8.GetBytes(message);byte[] outStr = md5.ComputeHash(result);string md5string = BitConverter.ToString(outStr).Replace("-", "");return md5string;}
/// <summary>/// 按字节数截取字符串的方法/// </summary>/// <param name="source">要截取的字符串(可空)</param>/// <param name="NumberOfBytes">要截取的字节数</param>/// <param name="encoding">System.Text.Encoding</param>/// <param name="suffix">结果字符串的后缀(超出部分显示为该后缀)</param>/// <returns></returns>public static string SubStringByBytes(this string source, int NumberOfBytes, System.Text.Encoding encoding, string suffix = ""){if (string.IsNullOrWhiteSpace(source) || source.Length == 0)return source;if (encoding.GetBytes(source).Length <= NumberOfBytes)return source;long tempLen = 0;StringBuilder sb = new StringBuilder();foreach (var c in source){Char[] _charArr = new Char[] { c };byte[] _charBytes = encoding.GetBytes(_charArr);if ((tempLen + _charBytes.Length) > NumberOfBytes){if (!string.IsNullOrWhiteSpace(suffix))sb.Append(suffix);break;}else{tempLen += _charBytes.Length;sb.Append(encoding.GetString(_charBytes));}}return sb.ToString();}
/// <summary>/// 复制一个对象/// </summary>/// <typeparam name="T"></typeparam>/// <param name="info"></param>/// <returns></returns>public static T Copy<T>(T info) where T : new(){string json = JsonHelper.SerializeObject(info);return JsonConvert.DeserializeObject<T>(json);}
/// <summary>/// 对象转Json字符串/// </summary>/// <typeparam name="T"></typeparam>/// <param name="data"></param>/// <returns></returns>public static string ToJson<T>(T data) where T : new(){return JsonHelper.SerializeObject(data);}
/// <summary>/// 实体类转Dictionary/// </summary>/// <typeparam name="T"></typeparam>/// <param name="info"></param>/// <returns></returns>public static Dictionary<string,string> ModelToDictionary<T>(this T info){Dictionary<string, string> dic = new Dictionary<string, string>();foreach (PropertyInfo pro in typeof(T).GetProperties()){string value = pro.GetValue(info, null)?.ToString();string name = pro.Name;if (string.IsNullOrEmpty(value)) continue;if (name == "Type") continue;dic.Add(name, value);}return dic;}
/// <summary>/// string扩展 rgb 转SolidColorBrush/// </summary>/// <param name="rgb"></param>/// <returns></returns>public static System.Windows.Media.SolidColorBrush ToSolidColorBrush(this string rgb){try{return new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(rgb));}catch (Exception){return System.Windows.Media.Brushes.Red;}}
示例:"#999999".ToSolidColorBrush()
转载于:https://www.cnblogs.com/zisai/p/11050749.html
c# 常用 Common相关推荐
- 3DsMax 创建阴影贴图的技术指南
创建阴影贴图的技术指南 概述 过程 步骤 1 步骤 2 步骤 3 步骤 4 步骤 5 步骤 6 步骤 7 步骤 8 步骤 9 步骤 10 步骤 11 步骤 12 步骤 13 步骤 14 概述 这里是一 ...
- ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl
ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库 源码下载 Maticsoft.DBUtility.dll 数据访问类库组件 源码下载 LtpPag ...
- common.php中路径,关于ThinkPHP中Common/common.php文件常用函数的功能分析
这篇文章主要介绍了ThinkPHP中Common/common.php文件常用函数功能,通过注释的形式详细分析了C方法.tag方法.B方法及autoload方法的功能与代码原理,需要的朋友可以参考下 ...
- Apache Common常用jar包
我们在日常开发中,不需要重复造轮子,Apache Common已经帮我们做了好多实用的jar包,直接拿来用就可以.也可以多研究下这些代码是怎么实现的. commons-logging.jar ---- ...
- DIDAO.Common --- 项目中的常用类及其中函数
DIDAO.Common --- 项目中的常用类及其中函数 常用函数: CommonHelper.cs using System; using System.Collections.Generic; ...
- 常用的Common助手类文件
工作中遇到的常用助手类,这里只有自己用到的一些东西. EmaileHelper using System; using System.Collections.Generic; using System ...
- python中common在哪个模块导入_python的常用模块之collections模块详解
认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...
- SiteFactory 通用程序集中(PowerEasy.Common)的常用字符串处理函数
以下是常用字符处理类StringHelper的各方法,这里给方法添加了简单的注释 --------------------- /// <summary> /// 使用逗号分割 ...
- git 常用操作命令(Common operation)
win10清除已登录账号密码方法 打开控制面板(Control Panel): 选择用户账户(User Accounts): 选择管理你的凭据(Credential Manager): 管理windo ...
- pytorch常用代码
20211228 https://mp.weixin.qq.com/s/4breleAhCh6_9tvMK3WDaw 常用代码段 本文代码基于 PyTorch 1.x 版本,需要用到以下包: impo ...
最新文章
- WEB技术架构的资源链接。
- SIFT算法总结:用于图像搜索
- Document的理解
- GCC + pthread
- OpenJudge NOI 1.16 08:石头剪刀布
- SQL server 存储过程实现统计赋值
- windows下boost库的基本使用方法
- python和java哪个好-Python和Java哪个好?有什么区别?
- 关于传奇MapInfo地图文件参数详细说明
- JS表单验证之正则表达式
- changeable和changeful_change的形容词是什么?
- html 复选框事件,HTML复选框选中与未选中触发事件的方法
- DISPLAY尚未设置
- Android自定义View(CustomCalendar-定制日历控件)
- Linux文件内容查阅 - cat, tac, nl, more, less, head, tail, od
- 他把自己估值上万亿美元的项目免费化了.....
- 计算机输入输出接口形式,输入输出接口-微计算机原理-电子发烧友网站
- 分享关于UE4中matinee工具的使用教程
- 【20】输入一个字符串,统计其中的字母、数字、其他字符的个数。
- 使用PfamScan的API对蛋白结构域进行注释