通过字典给类的实体属性赋值生成url字符串
private static Dictionary<string, string> SortedToDictionary(SortedDictionary<string, string> dicArrayPre, params string[] paras)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> temp in dicArrayPre)
if (!paras.Contains(temp.Key) && !string.IsNullOrEmpty(temp.Value))
dict.Add(temp.Key, temp.Value);
return dict;
}
private static string GetParmStr<T>(T model, bool isUrlEncode = false, params string[] paras)
{
var sortedDict = GetResultDictionary<T>(model);
var dict = FilterParams(sortedDict, paras);
return CreateLinkStringUrl(dict, isUrlEncode);
}
private static SortedDictionary<string, string> GetResultDictionary<T>(T model)
{
SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
List<string> paramsList = new List<string>();// 把实体类中的属性名称转换成List<string>
var modelDict = model.GetType().GetProperties().ToDictionary(a => a.Name.Substring(0, 1).ToLower() + a.Name.Substring(1));
foreach (var dkey in modelDict)
paramsList.Add(dkey.Key);
foreach (var p in paramsList)
{
System.Reflection.PropertyInfo pi = null;
if (modelDict.TryGetValue(p, out pi))
{
object obj = pi.GetValue(model, null);
if (pi != null && obj != null && !obj.ToString().Equals(""))
sortedDictionary.Add(p, obj.ToString());
}
}
return sortedDictionary;
}
private static Dictionary<string, string> FilterParams(SortedDictionary<string, string> sortedDictionary, params string[] paras)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> temp in sortedDictionary)
if (!paras.Contains(temp.Key) && !string.IsNullOrEmpty(temp.Value))
dict.Add(temp.Key, temp.Value);
return dict;
}
private static string CreateLinkStringUrl(Dictionary<string, string> dictionary, bool isUrlEncode = false)
{
StringBuilder stringBuilder = new StringBuilder();
if (isUrlEncode)
{
foreach (KeyValuePair<string, string> temp in dictionary)
stringBuilder.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value) + "&");
}
else
{
foreach (KeyValuePair<string, string> temp in dictionary)
stringBuilder.Append(temp.Key + "=" + temp.Value + "&");
}
int nLen = stringBuilder.Length;
stringBuilder.Remove(nLen - 1, 1);
return stringBuilder.ToString();
}
通过字典给类的实体属性赋值生成url字符串的更多相关文章
- 我写的RunTime函数之一,为类的某个属性赋值以及方法交换
1,为属性赋值 #import <UIKit/UIKit.h> @interface UIViewController (RunTime) - (BOOL)setPropertyVal ...
- Spring2.0-applicationContext.xml中使用el表达式给实体类属性赋值被当成字符串-遁地龙卷风
(-1)写在前面 这两天读<javaweb开发王者归来>,学到Spring的PropertyPlaceholderConfigurer时出现一个问题,我已${jdbc.name}的形式赋值 ...
- EF 实体类的制定属性不生成数据库字段
添加一个标签即可 [NotMapped] 没什么营养,就是防忘记
- spring-知识小结之注解为属性赋值
<1>.本类中的属性赋值 public class UserServiceImpl implements UserService { //按照类别赋值 // @Autowired //按照 ...
- Android利用反射机制为实体类属性赋值
在做android项目时,有时会遇到从网络上获取json类型数据,赋值给实体类,实体类属性少可以一个一个的赋值,如果实体类有很多属性,赋值可能就要耗很长的功夫了,幸好Java给我们提供了反射机制.下面 ...
- Java使用PropertyDescriptor获取实体类中私有属性的值,并给私有属性赋值
大家都知道Java类中的私有的(private)属性是获取不到的(即使使用继承依然获取不到),那如果非要获取私有属性的值怎么办呢?一般的做法是将该java类封装称为一个JavaBean,即封装该私有属 ...
- java反射对实体类取值和赋值
public static void checkDesignerEdit(Object dtos) throws Exception { Class dtosClass = dtos.getClass ...
- 关于entityframework 自动生成实体类中加验证的属性重新生成后属性被覆盖解决办法
1.手动创建一个部分类 (你可以手动创建 partial class, 内容为空) [MetadataType(typeof(AppleMetadata))] public partial class ...
- 实体类的枚举属性--原来支持枚举类型这么简单,没有EF5.0也可以
通常,我们都是在业务层和界面层使用枚举类型,这能够为我们编程带来便利,但在数据访问层,不使用枚举类型,因为很多数据库都不支持,比如我们现在用的SqlServer2008就不支持枚举类型的列,用的时候也 ...
随机推荐
- MYSQL 分表实践
基本条件: 无索引 主表 test_0 数据:一百万条 数据库引擎 InnoDb 分表 test_1...test_100 数据 每张一万条,一共一百万条 数据库引擎 InnoDb 流程: 主表中 ...
- Oracle PLSQL笔记(过程的创建和及调用)
过程(procedure): 用于在数据库中完成特定的操作或者任务.是一个PLSQL程序块,可以永久的保存在数据库中以供其他程序调用. 一.创建所需的表USERS create table users ...
- jQuery的delegate
jQuery的delegate 在网页开发的过程中经常遇到的一个需求就是点击一div内部做某些操作,而点击页面其它地方隐藏该div.比如很多导航菜单,当菜单展开的时候,就会要求点击页面其它非菜单地方, ...
- Thrift RPC实战(二) Thrift 网络服务模型
限于篇幅关系,在观察源码的时候,只列举了部分源代码 TServer类层次体系 TSimpleServer/TThreadPoolServer是阻塞服务模型 TNonblockingServer/THs ...
- 单例模式及C++实现代码
单例模式及C++实现代码 单例模式 单例模式,可以说设计模式中最常应用的一种模式了,据说也是面试官最喜欢的题目.但是如果没有学过设计模式的人,可能不会想到要去应用单例模式,面对单例模式适用的情况,可能 ...
- hdu 1498
每次只能消除一行或一列的相同颜色的气球, 求有多少种气球在k次内不能消除 求出每种气球最少需要多少次消除,就跟hdu 2119消除1用多少次是一样的问题 就是求有这种气球的行和列的最大匹配 #incl ...
- 杭电OJ——1011 Starship Troopers(dfs + 树形dp)
Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...
- 最近修bug的一点感悟
写在前面话 项目从13年1月份,现场开发,4月中旬,项目开发接近尾声,三个开发,留两个在现场,我被调回公司,5月份现场一同事离职,只有一个同事在开发,结果PM想让这一个同事承担余下的开发和bug工作, ...
- Mysql监控及优化
一.Mysql连接数 1.配置Mysql连接数: vim /etc/my.cnf [mysqld]下面修改 max_connections=1000 不写默认为100. wait_timeout=60 ...
- 项目在JDK1.8环境下的一个Bug
今天发现一个已有的项目在JDK1.8环境下运行时,会报错: The type java.util.Map$Entry cannot be resolved. It is indirectly refr ...