通过字典给类的实体属性赋值生成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 流程: 主表中 ...
- OpenCascade简介
OpenCascade简介 Overview of OpenCascade Library eryar@163.com 摘要Abstract:对OpenCascade库的功能及其实现做简要介绍. ...
- JavaEE:Eclipse开发工具的相关使用和XML技术
Eclipse开发工具的知识点1.工程的属性(properties)1)Text file encoding 工程编码(在导入其他工程时,注意编码类型一致)2)Java build path设置cl ...
- 那天有个小孩教我WCF[一][1/3]
那天有个小孩教我WCF[一][1/3] 既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈 前言: 第一篇嘛,不细讲,步步教你创建一个简单SOA案例,对WCF有个基本的认识,我不 ...
- 深入剖析Linux I/O操作与标准I/O操作区别与联系
文件I/O:open creat close lseek read write dup dup2 sync fsync fcntl ioctl 所有函数都是针对文件描述符. 所有的都是原子操作,这个在 ...
- Windows 8.1 Preview的新功能和新API
http://msdn.microsoft.com/en-us/library/windows/apps/bg182410 App打包 新的App程序包将使App的提交更简单.资源包可以让你提供附加的 ...
- LaTeX入门教程
LaTeX(LATEX,音译"拉泰赫")是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用 ...
- JavaScript星级评分
事件onmouseover <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- ACM博弈问题小试
题目: 取石子(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 一天,TT在寝室闲着无聊,和同寝的人玩起了取石子游戏,而由于条件有限,他/她们是用旺仔小馒头当作 ...
- Android版本和API Level的对应关系
Platform Version API Level VERSION_CODE Notes Android 4.4 KITKAT Platform Highlights Android 4.3 JEL ...