using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
namespace SaftSQL
{
public class SetterWrapper<TTarget, TValue>
{
private Action<TTarget, TValue> _setter;
public SetterWrapper(PropertyInfo propInfo)
{
if (propInfo == null)
throw new ArgumentNullException("propertyInfo");
if (!propInfo.CanWrite)
throw new NotSupportedException("属性是只读或Private Setter");
MethodInfo setMethod = propInfo.GetSetMethod(true);
_setter = (Action<TTarget, TValue>)Delegate.CreateDelegate(typeof(Action<TTarget, TValue>), null, setMethod);
}
public void SetValue(TTarget target, TValue val)
{
if (_setter != null)
{
_setter(target, val);
}
}
}
public class GetterWrapper<TTarget, TValue>
{
private Func<TTarget, TValue> _getter;
public GetterWrapper(PropertyInfo propInfo)
{
if (propInfo == null)
throw new ArgumentNullException("propertyInfo");
if (!propInfo.CanRead)
throw new NotSupportedException("属性是不可读或Private Getter");
MethodInfo getMethod = propInfo.GetGetMethod(true);
_getter = (Func<TTarget, TValue>)Delegate.CreateDelegate(typeof(Func<TTarget, TValue>), null, getMethod);
}
public TValue GetValue(TTarget target)
{
if (_getter != null)
{
return _getter(target);
}
return default(TValue);
}
}
public abstract class BaseQueryFilter
{
public void SafeSubmit<T>() where T : BaseQueryFilter
{
PropertyInfo[] propInfoArr = this.GetType().GetProperties();
foreach (var propInfo in propInfoArr)
{
if (propInfo.PropertyType == typeof(System.String))
{
GetterWrapper<T, string> getter = new GetterWrapper<T, string>(propInfo);
string val = getter.GetValue(this as T);
if (string.IsNullOrEmpty(val)) continue;
if (val.IndexOf("'") > -1)
{
SetterWrapper<T, string> setter = new SetterWrapper<T, string>(propInfo);
setter.SetValue(this as T, val.Replace("'", "''"));

}

}
}
}
}
}

用法:

class OrderFilter

{

public string ClientPhone{get;set;}

public string ClientName{get;set;}
}

void Main()

{

OrderFilter orderFilter = new OrderFilter()
{
ClientName="'123"
};

orderFilter.SafeSubmit();
}

最新文章

  1. Lintcode 175. 翻转二叉树
  2. angularJS- $http请求
  3. Sizzle选择器引擎介绍
  4. unity3d拓展编辑器MenuItem的使用
  5. 第一次听到了docker
  6. nexcel 读取 excel
  7. Genymotion无法启动Virtual Box
  8. 通过Excel来集中管理资源文件
  9. 【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位网友补充完善
  10. sdut 3-4 长方形的周长和面积计算
  11. Vmware Vcenter6.0 全新安装及群集配置介绍
  12. 为Github项目创建文档
  13. Android Studio中配置及使用OpenCV示例
  14. 笔记整理--Http-Cookie
  15. Android Studio升级到3.4遇到的问题总结
  16. 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,nloglogutil
  17. API接口设计,rest,soap
  18. YAML 知识点
  19. 怎样使用github
  20. hdu 5089 使做对k-1题最大概率的选题方案

热门文章

  1. Spring 4 bak
  2. C#不同窗体间通信,数据传递
  3. php基础03:数据类型
  4. Java从0开始学——字符串
  5. 从0开始学Java——JSP&amp;Servlet——如何部署web应用程序
  6. [CareerCup] 13.1 Print Last K Lines 打印最后K行
  7. windows2003 单网卡 搭建vpn ,windows 2008 R2 类似吧。网上转的,自己加了点经验总结
  8. Django实际站点项目开发经验谈
  9. jdbc基础 (三) 大文本、二进制数据处理
  10. [C#]Hosting Process (vshost.exe)