| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Collections;
- using System.Text;
- using System.Xml;
- namespace DopInterfacePlatform
- {
- public static class StringHelper
- {
- private static int[] numbers = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- /// <summary>
- /// 随机字符串数组集合
- /// </summary>
- private static readonly string[] NonceStrings = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
- /// <summary>
- /// 四舍五入取整数部分
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static int StringToIntMathRound(this object @this)
- {
- string s_this = @this.ToString();
- int i_result = 0;
- if (s_this.Contains("."))
- {
- var tS =s_this.Split(".");
- i_result = Convert.ToInt32(tS[0]);
- if (Convert.ToInt32(tS[1].Substring(0, 1)) > 4)
- {
- i_result += 1;
- }
- }
- else
- {
- i_result = Convert.ToInt32(@this);
- }
- return i_result;
- }
-
- /// <summary>
- /// 生成指定长度的随机字符串
- /// </summary>
- /// <returns></returns>
- public static string CreateNonceStr(this int stringLenght)
- {
- Random random = new Random();
- var sb = new StringBuilder();
- var length = NonceStrings.Length;
- for (int i = 0; i < stringLenght; i++)
- {
- //通过random获得的随机索引到,NonceStrings数组中获取对应数组值
- sb.Append(NonceStrings[random.Next(length - 1)]);
- }
- return sb.ToString();
- }
-
- /// <summary>
- /// 获取指定长度的随机数
- /// </summary>
- /// <returns></returns>
- public static string GetRandom(this string @prexString, int lenght)
- {
- string result = prexString;
- Random rd = new Random();
- for (int i = 0; i < lenght - prexString.Length; i++)
- {
- int RandKey = rd.Next(100, 999);
- var index = RandKey % 10;
- result += numbers[index].ToString();
- }
- return result;
- }
- /// <summary>
- /// decimal取指定位小数返回
- /// </summary>
- /// <param name="this"></param>
- /// <param name="decimals"></param>
- /// <returns></returns>
- public static decimal DecimalsFormat(this decimal @this,int decimals=2)
- {
- return Math.Round(@this, decimals);
- }
- /// <summary>
- /// 涉及到金额部分把最后的0 去掉
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static string CashFormat(this object @this)
- {
- string cashValue = @this.ToString();
- if (cashValue != null && cashValue.Contains(".")) //
- {
- while (cashValue.EndsWith("0"))
- {
- cashValue = cashValue.Substring(0, cashValue.Length - 1);
- }
- if (cashValue.EndsWith("."))
- cashValue = cashValue.Substring(0, cashValue.Length - 1);
- }
- return cashValue;
- }
- /// <summary>
- /// 判断是否为空
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static bool IsNullOrWhiteSpace(this string @this)
- {
- if (@this == null) return true;
- return string.IsNullOrWhiteSpace(@this.Trim()) || @this == "null";
- }
- /// <summary>
- /// tostring的另一个版本,当是null字符串时返回空串
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static string ToSelfOrNullString(this string @this)
- {
- if (@this == null) return "";
- return string.IsNullOrWhiteSpace(@this.Trim()) || @this == "null" ? "" : @this;
- }
- public static Hashtable GetProperties<T>(this T @userInfo)
- {
- Hashtable hashtable = new Hashtable();
- foreach (System.Reflection.PropertyInfo p in userInfo.GetType().GetProperties())
- {
- hashtable.Add(p.Name, p.GetValue(userInfo));
- }
- return hashtable;
- }
- /// <summary>
- /// 去掉Emoji表情
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static string RemoveEmoji(this string @this)
- {
- foreach (var strTmp in @this)
- {
- byte[] bts = Encoding.UTF32.GetBytes(strTmp.ToString());
-
- if (bts[0].ToString() == "253"&& bts[1].ToString() == "255")
- {
- @this = @this.Replace(strTmp.ToString(), "");
- }
-
- }
- return @this;
- }
- /// <summary>
- /// 将Json字符串转换为对应的Json对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="this"></param>
- /// <returns></returns>
- public static T ToJsonObject<T>(this string @this)
- {
- return JsonConvert.DeserializeObject<T>(@this);
- }
- public static string DateTimeOfDayWeek(this string @this)
- {
- try
- {
- return ((int) Convert.ToDateTime(@this).DayOfWeek).ToString();
- }
- catch (Exception e)
- {
- return "";
- }
- }
- public static string YuanToFen(this string money)
- {
- return Convert.ToInt64(Convert.ToDecimal(money) * 100).ToString();
- }
-
- public static string FenToYuan(this int money)
- {
- return (Convert.ToDecimal(money)/100).ToString();
- }
- /// <summary>
- /// 将对象转换为string字符串
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static string ToJsonString(this object @this)
- {
- return JsonConvert.SerializeObject(@this);
- }
- /// <summary>
- /// 排序字段
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static Dictionary<string, object> SortDictionary(this Dictionary<string, object> @this)
- {
- Dictionary<string, object> keyValues = new Dictionary<string, object>();
- foreach (var item in @this)
- {
- if (item.Value == null || item.Value.ToString() == "")
- {
- continue;
- }
- if(item.Key== "PatType")
- {
- }
- if (item.Value.GetType().Name == "JObject")
- {
- JObject j_object = item.Value as JObject;
- keyValues = keyValues.Concat(j_object.JObjectToDictionary()).ToDictionary(k => k.Key, v => v.Value);
- }
- else
- {
- keyValues.Add(item.Key, item.Value);
- }
- }
- return keyValues.OrderBy(p => p.Key).Where(d => d.Value != null).ToDictionary(p => p.Key, o => o.Value);
- }
- /// <summary>
- /// JObject to Dictionary<string, object>
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- private static Dictionary<string, object> JObjectToDictionary(this JObject @this)
- {
- Dictionary<string, object> keyValues = new Dictionary<string, object>();
- foreach (var item in @this)
- {
- keyValues.Add(item.Key, item.Value);
- //不考虑有多层的情况
- //if (item.Value.GetType().Name == "JObject")
- //{
- // JObject j_object = item.Value as JObject;
- // //还有子项目
- // keyValues = keyValues.Concat(j_object.JObjectToDictionary()).ToDictionary(k => k.Key, v => v.Value);
- //}
- //else
- //{
- // keyValues.Add(item.Key, item.Value);
- //}
- }
- return keyValues;
- }
- /// <summary>
- /// 转换成签名串
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static string ToSignString(this object @this)
- {
- var objName = @this.GetType().Name;
- if (objName == "DateTime")
- {
- return Convert.ToDateTime(@this).ToString("yyyy-MM-dd HH:mm:ss");
- }
- else
- {
- return @this.ToString();
- }
- }
- /// <summary>
- /// BASE64编码
- /// </summary>
- /// <param name="un_code_string"></param>
- /// <param name="code_type"></param>
- /// <returns></returns>
- public static string EncodeBase64(this string un_code_string,string code_type= "utf-8")
- {
- byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(un_code_string);
- string encode;
- try
- {
- encode = Convert.ToBase64String(bytes);
- }
- catch
- {
- encode = un_code_string;
- }
- return encode;
- }
- /// <summary>
- /// BASE64解码
- /// </summary>
- /// <param name="code_string"></param>
- /// <param name="code_type"></param>
- /// <returns></returns>
- public static string DecodeBase64(this string code_string,string code_type = "utf-8")
- {
- byte[] bytes = Convert.FromBase64String(code_string);
- string decode;
- try
- {
- decode = Encoding.GetEncoding(code_type).GetString(bytes);
- }
- catch
- {
- decode = code_string;
- }
- return decode;
- }
- /// <summary>
- /// xml格式的字符串转换成对象
- /// </summary>
- /// <param name="xmlStr"></param>
- /// <returns></returns>
- public static T XmlToObject<T>(this string xmlStr) where T : new()
- {
- try
- {
- T obj = new T();
- var repType = typeof(T);
- XmlDocument document = new XmlDocument();
- document.LoadXml(xmlStr); //加载Xml文件
- XmlElement node = document.DocumentElement; //xml的根标签
- var properties = repType.GetProperties();
- foreach (var itemProp in properties)
- {
- #region current type is List
- if (itemProp.PropertyType.FullName.Contains("System.Collections.Generic.List"))
- {
- object array = new object();
- var arryLength = 0;
- var notNullLength = 0;
- var arryType = itemProp.PropertyType.UnderlyingSystemType;
- var objList = itemProp.GetValue(obj, null) as System.Collections.IEnumerable;
- var enumt = objList.GetEnumerator();
- //enumt.
- var currentType = itemProp.PropertyType.GetGenericArguments()[0];
- foreach (XmlNode xmlitem in node.ChildNodes)
- {
- if (xmlitem.Name == itemProp.Name)
- {
- arryLength++;
- }
- }
- if (arryLength > 0)
- {
- var arrayModel = arryType.InvokeMember("Set", System.Reflection.BindingFlags.CreateInstance, null, array, new object[] { arryLength }) as System.Collections.IList;
- foreach (XmlNode item in node.ChildNodes)
- {
- //current type is array
- if (item.Name == itemProp.Name)
- {
- var model = currentType.Assembly.CreateInstance(currentType.FullName); // arryType.GetElementType().Assembly.CreateInstance(currentType.FullName);
- SetArray(item.ChildNodes, model, true);
- arrayModel.Add(model);
- //arrayModel[notNullLength] = model;
- notNullLength++;
- }
- }
- itemProp.SetValue(obj, arrayModel, null);
- }
- continue;
- }
- #endregion
- var baseType = itemProp.PropertyType.BaseType.Name;
- if (baseType == "Array")
- {
- #region Current type is Array
- object array = new object();
- var arryLength = 0;
- var notNullLength = 0;
- var arryType = itemProp.PropertyType.UnderlyingSystemType;
- foreach (XmlNode xmlitem in node.ChildNodes)
- {
- if (xmlitem.Name == itemProp.Name && xmlitem.ChildNodes!=null && xmlitem.ChildNodes.Count>0)
- {
- arryLength++;
- }
- }
- if (arryLength > 0)
- {
- var arrayModel = arryType.InvokeMember("Set", System.Reflection.BindingFlags.CreateInstance, null, array, new object[] { arryLength }) as System.Collections.IList;
- foreach (XmlNode item in node.ChildNodes)
- {
- //current type is array
- if (item.Name == itemProp.Name && item.ChildNodes!=null && item.ChildNodes.Count>0)
- {
- var model = arryType.GetElementType().Assembly.CreateInstance(arryType.GetElementType().FullName);
- SetArray(item.ChildNodes, model);
- arrayModel[notNullLength] = model;
- notNullLength++;
- }
- }
- itemProp.SetValue(obj, arrayModel, null);
- }
- #endregion
- }
- else
- {
- #region Current type isn't Array
- foreach (XmlNode item in node.ChildNodes)
- {
- #region Current type is Number
- if (itemProp.Name == item.Name && (itemProp.PropertyType == typeof(long) || itemProp.PropertyType == typeof(int) || itemProp.PropertyType == typeof(string)))
- {
- if (itemProp.PropertyType == typeof(int) || itemProp.PropertyType == typeof(long))
- {
- if (!string.IsNullOrEmpty(item.InnerText))
- {
- if (itemProp.PropertyType == typeof(int))
- {
- itemProp.SetValue(obj, Convert.ToInt32(item.InnerText), null);
- }
- else
- {
- itemProp.SetValue(obj, Convert.ToInt64(item.InnerText), null);
- }
- }
- else
- {
- itemProp.SetValue(obj, 0, null);
- }
- }
- else
- {
- itemProp.SetValue(obj, item.InnerText, null);
- }
- }
- #endregion
- #region Current type is Model
- if (itemProp.PropertyType != typeof(long) && itemProp.PropertyType != typeof(string) && itemProp.PropertyType != typeof(int) && itemProp.PropertyType.Name == item.Name && item.HasChildNodes && item.FirstChild.NodeType == System.Xml.XmlNodeType.Element)
- {
- var modelType = itemProp.PropertyType.UnderlyingSystemType;
- var model = modelType.Assembly.CreateInstance(modelType.FullName);
- SetArray(item.ChildNodes, model);
- itemProp.SetValue(obj, model, null);
- }
- #endregion
- }
- #endregion
- }
- }
- repType = obj.GetType();
- return obj;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #region Set array value
- private static Object SetArray(XmlNodeList xmlNodeList, object obj, bool isList = false)
- {
- try
- {
- var type = obj.GetType();
- var properties = type.GetProperties();
- foreach (var itemProp in properties)
- {
- //if (isList)
- if (itemProp.PropertyType.FullName.Contains("System.Collections.Generic.List"))
- {
- #region Current type is List
- object array = new object();
- var arryLength = 0;
- var notNullLength = 0;
- var arryType = itemProp.PropertyType.UnderlyingSystemType;
- var currentType = itemProp.PropertyType.GetGenericArguments()[0];
- foreach (XmlNode xmlitem in xmlNodeList)
- {
- if (xmlitem.Name == itemProp.Name)
- {
- arryLength++;
- }
- }
- if (arryLength > 0)
- {
- var arrayModel = arryType.InvokeMember("Set", System.Reflection.BindingFlags.CreateInstance, null, array, new object[] { arryLength }) as System.Collections.IList;
- foreach (XmlNode item in xmlNodeList)
- {
- //current type is array
- if (item.Name == itemProp.Name)
- {
- var model = currentType.Assembly.CreateInstance(currentType.FullName); // var model = arryType.GetElementType().Assembly.CreateInstance(arryType.GetElementType().FullName);
- SetArray(item.ChildNodes, model, true);
- arrayModel.Add(model);
- notNullLength++;
- }
- }
- itemProp.SetValue(obj, arrayModel, null);
- }
- #endregion
- return obj;
- }
- var baseType = itemProp.PropertyType.BaseType.Name;
- if (baseType == "Array")
- {
- #region Current type is Array
- object array = new object();
- var arryLength = 0;
- var notNullLength = 0;
- var arryType = itemProp.PropertyType.UnderlyingSystemType;
- foreach (XmlNode xmlitem in xmlNodeList)
- {
- if (xmlitem.Name == itemProp.Name)
- {
- arryLength++;
- }
- }
- if (arryLength > 0)
- {
- var arrayModel = arryType.InvokeMember("Set", System.Reflection.BindingFlags.CreateInstance, null, array, new object[] { arryLength }) as System.Collections.IList;
- foreach (XmlNode item in xmlNodeList)
- {
- //current type is array
- if (item.Name == itemProp.Name)
- {
- var model = arryType.GetElementType().Assembly.CreateInstance(arryType.GetElementType().FullName);
- SetArray(item.ChildNodes, model);
- arrayModel[notNullLength] = model;
- notNullLength++;
- }
- }
- itemProp.SetValue(obj, arrayModel, null);
- }
- #endregion
- }
- else
- {
- foreach (XmlNode item in xmlNodeList)
- {
- #region Current type is Number
- if (itemProp.Name == item.Name && (itemProp.PropertyType == typeof(long) || itemProp.PropertyType == typeof(int) || itemProp.PropertyType == typeof(string)))
- {
- if (itemProp.PropertyType == typeof(int) || itemProp.PropertyType == typeof(long))
- {
- if (!string.IsNullOrEmpty(item.InnerText))
- {
- if (itemProp.PropertyType == typeof(int))
- {
- itemProp.SetValue(obj, Convert.ToInt32(item.InnerText), null);
- }
- else
- {
- itemProp.SetValue(obj, Convert.ToInt64(item.InnerText), null);
- }
- }
- else
- {
- itemProp.SetValue(obj, 0, null);
- }
- }
- else
- {
- itemProp.SetValue(obj, item.InnerText, null);
- }
- }
- #endregion
- #region Current type is Model
- if (itemProp.PropertyType != typeof(long) && itemProp.PropertyType != typeof(int) && itemProp.PropertyType != typeof(string) && itemProp.PropertyType.Name == item.Name && item.HasChildNodes && item.FirstChild.NodeType == System.Xml.XmlNodeType.Element)
- {
- var modelType = itemProp.PropertyType.UnderlyingSystemType;
- var model = modelType.Assembly.CreateInstance(modelType.FullName);
- SetArray(item.ChildNodes, model);
- itemProp.SetValue(obj, model, null);
- }
- #endregion
- }
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- return obj;
- }
- #endregion
- }
- }
|