首页 技术 正文
技术 2022年11月12日
0 收藏 324 点赞 4,280 浏览 3791 个字
 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 = p.GetValue(o, null);
if (objValue != null)
{
if (objValue.GetType() == typeof(System.String))
{
if (isEscape)
p.SetValue(o, Microsoft.JScript.GlobalObject.escape(objValue.ToString()), null);
else
p.SetValue(o, Microsoft.JScript.GlobalObject.unescape(objValue.ToString()), null);
}
}
}
}
} /// <summary>
/// js编码解码
/// </summary>
/// <param name="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 = p.GetValue(o, null);
if (objValue != null)
{
if (objValue.GetType() == typeof(System.String))
{
p.SetValue(o, Microsoft.JScript.GlobalObject.escape(objValue.ToString()), null);
}
}
}
}
} public static void ToJSDecodeEntity<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 = p.GetValue(o, null);
if (objValue != null)
{
if (objValue.GetType() == typeof(System.String))
{
p.SetValue(o, Microsoft.JScript.GlobalObject.unescape(objValue.ToString()), null);
}
}
}
}
} public static void ToJSEncodeTable(this DataSet o)
{
foreach (DataTable t in o.Tables)
{
ToJSEncodeTable(t);
}
} public static void ToJSDecodeTable(this DataSet o)
{
foreach (DataTable t in o.Tables)
{
ToJSDecodeTable(t);
}
} public static void ToJSEncodeTable(this DataTable o)
{
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++)
{
o.Rows[i][j] = Microsoft.JScript.GlobalObject.escape(o.Rows[i][j].ToString());
}
}
}
} public static void ToJSDecodeTable(this DataTable o)
{
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++)
{
o.Rows[i][j] = Microsoft.JScript.GlobalObject.unescape(o.Rows[i][j].ToString());
}
}
}
} #endregion
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,093
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,569
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,417
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,190
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908