检测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();
}
检测ADO.net拼接字符串中非法字符的更多相关文章
- Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例
1. 首先我们看看统计字符串中每个字符出现的次数的案例图解: 2. 代码实现: (1)需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5) ...
- C语言:根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,-主函数中放入一个带头节点的链表结构中,h指向链表的头节点。fun函数找出学生的最高分-使用插入排序法对字符串中的字符进行升序排序。-从文件中找到指定学号的学生数据,读入次学生数据,
//根据形参c中指定的英文字母,按顺序打印出若干后继相邻字母,输出字母的大小与形参c一致,数量由形参d指定.例如:输入c为Y,d为4,则输出ZABC. #include <stdio.h> ...
- Python2.7.3移除字符串中重复字符(一)
移除重复字符很简单,这里是最笨,也是最简单的一种.问题关键是理解排序的意义: # coding=utf-8 #learning at jeapedu in 2013/10/26 #移除给定字符串中重复 ...
- 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数
有一道经典的字符串处理的问题,统计一个字符串中每个字符出现的次数. 用es6的Array.reduce()函数配合“...”扩展符号可以更方便的处理该问题. s='abananbaacnncn' [. ...
- Excel中如何截取字符串中指定字符后的部分字符
1.如何给某列属性为时间整体加一个时间值: 场景一:假如我有一个excel中的某一列如下图所示,如何将该列的时间(用B代替整列)整体加一分钟呢?方法很简单,在空白单元格填写时间格式图中A所示 ...
- fortran中提取字符串中可见字符的索引
fortran中常常需要提取字符串中可见字符的索引,下面是个小例子: !============================================================= su ...
- python中是否有单独的字符类型,通过下标的方式表示字符串中的字符
说明: 在python中,没有单独的字符类型,一个字符呢就是一个大小为1的字符串. 并且可以通过下标的方式,表示字符串中的字符. 操作过程: 1.通过[ ]的方式表示字符串中的第几个字符 >&g ...
- 统计字符串中每个字符出现的次数(Python)
#统计字符串中每个字符出现的次数 以The quick brown fox jumps over the lazy dog为例 message='The quick brown fox jumps o ...
- Java中统计字符串中各个字符出现的次数
import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class TreeMapDemo ...
随机推荐
- 转载:有关SQL server connection Keep Alive 的FAQ(3)
转载:http://blogs.msdn.com/b/apgcdsd/archive/2012/06/07/sql-server-connection-keep-alive-faq-3.aspx 这个 ...
- C语言 预处理一(文件包含--#include)
//预处理命令不需要在结束末尾加":" //#inlude可以包含任意类型的文件 //#inlude 将一个源文件的全部内容包含到另一个源文件中,成为它的一个部分,文件包含的一般格 ...
- Jquery easyui tree的使用
这个ui用的一切都是json数据.树也是如此! 后台需要返回与格式匹配的json数据才能正确加载树. 页面定义一个ui: <ul id="messageInfoAddTree" ...
- 2016北京PHP39期 ThinkPHP Discuz Dedecms 微信开发视频教程
2016北京PHP39期 ThinkPHP Discuz Dedecms 微信开发视频教程 所有项目均带有软件,笔记,视频,源码 日期 课程(空内容代表放假) 2015/7/10 星期五 开学典礼 ...
- ibatis 到 MyBatis区别(zz)
简介: 本文主要讲述了 iBatis 2.x 和 MyBatis 3.0.x 的区别,以及从 iBatis 向 MyBatis 移植时需要注意的地方.通过对本文的学习,读者基本能够了解 MyBatis ...
- VS一般设置(字体,背景色)
字体 打开工具=>环境=>字体和颜色,字体:Consolas,大小:13 背景色 缩进设置 工具=>文本编辑器=>纯文本=>制表符=>保留制表符
- Linq 中查询一个表中指定的字段
//Linq中查询一个表中指定的几个字段: ); // FindAllItems()为查询对应表的所有数据的方法: // Where 里面为查询条件 // Select 为查询的筛选条件 new{} ...
- [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1295 分析:很巧妙的一道spfa从搜索的角度是搜索在所有1中搜索删除哪T个1,对整个图询问,这 ...
- C#基础知识系列五(构造函数)
前言 本节主要来学习下实例构造函数.私有构造函数和静态构造函数. 构造函数的作用: 1.当一个类在new实例化对象时,可调用构造函数进行初始化对象. 2.不带参数的构造函数称为“默认构造函数”. 无论 ...
- 茗洋Easy UI 1.3.2 部分问题解决系列专题[Combo模糊匹配中文问题 修复]
本次给大家带来的EasyUI的我研究拓展的新特性 我使用的是 EasyUI 1.3.2版本的,项目是ASP.NET MVC3,但是本篇讲解用不上ASP.NET MVC,仅仅修改官方Demo你就知道怎 ...