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 == )
return source; if (encoding.GetBytes(source).Length <= NumberOfBytes)
return source; long tempLen = ;
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的更多相关文章
- 常用Common集合
Log using System; using System.IO; using System.Linq; using System.Text; namespace Common { public c ...
- LAMP架构(二)
第十八次课 LAMP架构(二) 目录 一.Apache默认虚拟主机 二.Apache用户认证 三.域名跳转 四.Apache访问日志 五.访问日志不记录静态文件 六.访问日志切割 七.静态元素过期时间 ...
- ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl
ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库 源码下载Maticsoft.DBUtility.dll 数据访问类库组件 源码下载LtpPageC ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl (转)
ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库 源码下载Maticsoft.DBUtility.dll 数据访问类库组件 源码下载LtpPageC ...
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- Linux 常用命令 (common commands for linux)
Linux 常用命令 (Common Commands For Linux) 1.声明,此文章仅写基于 Bash shell 常用的命令,如果遇上命令在使用过程中提示没有,可能随着更新,命令也被替换掉 ...
- selenium.webdriver.common.keys 模块中常用的变量
表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...
- DIDAO.Common --- 项目中的常用类及其中函数
常用函数: CommonHelper.cs using System; using System.Collections.Generic; using System.IO; using System. ...
随机推荐
- OSX:不同OSX版本号的标记可能不兼容
现象: 依据測试,中文OS X 10.9和中文10.10的文件标记彼此不兼容. 也就是说.比方在10.9中的颜色标记,在10.10DP2中不能删除,但能够加入/删除10.10自己的颜色标记,反之亦然. ...
- 【bzoj3676】[Apio2014]回文串 —— 回文自动机的学习
写题遇上一棘手的题,[Apio2014]回文串,一眼看过后缀数组+Manacher.然后就码码码...过是过了,然后看一下[Status],怎么慢这么多,不服..然后就搜了一下,发现一种新东西——回文 ...
- Centos7 防火墙firewalld配置
开启80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 出现success表明添加成功 移除某个端口 firewall-cmd ...
- 【Android】Android输入子系统【转】
本文转载自:https://www.cnblogs.com/lcw/p/3506110.html Linux输入子系统回顾 1:为什么要回顾linux输入子系统?这个问题后面自然就知道了 1.linu ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- MIPI接口
接口 分辨率 说明 RGB 800*480以下 大部分AP均支持RGB接口,此类LCD在低端平板广泛使用 LVDS 1024*768及以上 主要通过转换芯片将RGB等专程LVDS来支持:少量AP直接集 ...
- A bad vacation
My story happened in the winter of 2012, the first year I began to work in Beijing. It was a cold we ...
- bzoj1509
树的直径 我先开始以为是个图,想想并不知道什么求图的直径的方法,结果是棵树 那么直觉告诉我们是在直径上面,实际上就是直径+min(i->u,i->v),扫一遍就行了 #include< ...
- mysql通用分页存储过程遇到的问题(转载)
mysql通用分页存储过程遇到的问题(转载) http://www.cnblogs.com/daoxuebao/archive/2015/02/09/4281980.html