检测ADO.net拼接字符串中非法字符
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();
}
最新文章
- Lintcode 175. 翻转二叉树
- angularJS- $http请求
- Sizzle选择器引擎介绍
- unity3d拓展编辑器MenuItem的使用
- 第一次听到了docker
- nexcel 读取 excel
- Genymotion无法启动Virtual Box
- 通过Excel来集中管理资源文件
- 【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位网友补充完善
- sdut 3-4 长方形的周长和面积计算
- Vmware Vcenter6.0 全新安装及群集配置介绍
- 为Github项目创建文档
- Android Studio中配置及使用OpenCV示例
- 笔记整理--Http-Cookie
- Android Studio升级到3.4遇到的问题总结
- 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,nloglogutil
- API接口设计,rest,soap
- YAML 知识点
- 怎样使用github
- hdu 5089 使做对k-1题最大概率的选题方案
热门文章
- Spring 4 bak
- C#不同窗体间通信,数据传递
- php基础03:数据类型
- Java从0开始学——字符串
- 从0开始学Java——JSP&;Servlet——如何部署web应用程序
- [CareerCup] 13.1 Print Last K Lines 打印最后K行
- windows2003 单网卡 搭建vpn ,windows 2008 R2 类似吧。网上转的,自己加了点经验总结
- Django实际站点项目开发经验谈
- jdbc基础 (三) 大文本、二进制数据处理
- [C#]Hosting Process (vshost.exe)