首页 > 代码库 > C# 对JS编码/解码进行转换
C# 对JS编码/解码进行转换
public static class Extension { #region [编码/解码统一转换] /// <summary> /// /// </summary> /// <param name="str"></param> /// <param name="isEscape">True为Encode;False为Decode</param> /// <returns></returns> public static string JSCodingString(this string str, bool isEscape = false) { if (isEscape) return Microsoft.JScript.GlobalObject.escape(str); else return Microsoft.JScript.GlobalObject.unescape(str); } /// <summary> /// js编码解码 /// </summary> /// <param name="lsto"></param> /// <param name="isEscape"></param> public static void JSCodingEntityList<T>(this List<T> lsto, bool isEscape = false) { foreach (object o in lsto) { JSCodingEntity(o, isEscape); } } /// <summary> /// js编码解码 /// </summary> /// <param name="o"></param> /// <param name="isEscape"></param> public static void JSCodingEntity<T>(this T o, bool isEscape = false) { object objValue; System.Reflection.PropertyInfo[] propertys = o.GetType().GetProperties(); foreach (System.Reflection.PropertyInfo p in propertys) { if (p.PropertyType == typeof(System.String)) { objValue = http://www.mamicode.com/p.GetValue(o, null);"o"></param> /// <param name="isEscape"></param> public static void JSCodingTable(this DataSet o, bool isEscape = false) { foreach (DataTable t in o.Tables) { JSCodingEntity(t, isEscape); } } /// <summary> /// js编码解码 /// </summary> /// <param name="o"></param> /// <param name="isEscape"></param> public static void JSCodingTable(this DataTable o, bool isEscape = false) { for (int j = 0; j < o.Columns.Count; j++) { if (o.Columns[j].DataType == typeof(System.String)) { for (int i = 0; i < o.Rows.Count; i++) { if (isEscape) o.Rows[i][j] = Microsoft.JScript.GlobalObject.escape(o.Rows[i][j].ToString()); else o.Rows[i][j] = Microsoft.JScript.GlobalObject.unescape(o.Rows[i][j].ToString()); } } } } #endregion #region [重构:将原有编码/解码采用独立的函数方式。] public static string ToJSEncodeString(this string str) { return Microsoft.JScript.GlobalObject.escape(str); } public static string ToJSDecodeString(this string str) { return Microsoft.JScript.GlobalObject.unescape(str); } public static void ToJSEncodeEntityList<T>(this List<T> lsto) { foreach (object o in lsto) { ToJSEncodeEntity(o); } } public static void ToJSDecodeEntityList<T>(this List<T> lsto) { foreach (object o in lsto) { ToJSDecodeEntity(o); } } public static void ToJSEncodeEntity<T>(this T o) { object objValue; System.Reflection.PropertyInfo[] propertys = o.GetType().GetProperties(); foreach (System.Reflection.PropertyInfo p in propertys) { if (p.PropertyType == typeof(System.String)) { objValue = http://www.mamicode.com/p.GetValue(o, null);>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。