/// <summary> /// 替换掉对象属性值中的SQL关键字 /// </summary> public static void ReplaceSQLKeywords(this object value) { if (value.IsReferenceObject()) { Action <object> fn = null; fn = (obj) => { foreach (var item in ObjectHelper.GetProperties(obj)) { try { var tmpValue = item.GetValue(obj); if (tmpValue != null) { if (item.PropertyType == typeof(string)) { item.SetValue(obj, ConvertHelper.GetString(tmpValue).ReplaceSQLKeywords()); } else if (!item.PropertyType.IsValueType) { fn(tmpValue); } } } catch { } } }; fn(value); } }
/// <summary> /// 使用对象替换字符串里以对象属性命名的动态变量[如"{name}"] /// </summary> public static string ReplaceVariableByObject(this string str, object obj) { if (!string.IsNullOrEmpty(str) && obj != null) { var tmpProperties = ObjectHelper.GetProperties(obj); foreach (var item in tmpProperties) { str = str.Replace("{" + item.Name + "}", ConvertHelper.GetString(item.GetValue(obj))); } } return(str); }