C# 处理实体类赋值(获取嵌套类型,支持list 自定义类型)
public static T AESEncrypt<T>(T obj) where T : class
{
if (obj == null)
{
return obj;
} var properties = typeof(T).GetProperties(); foreach (System.Reflection.PropertyInfo info in properties)
{
if (info.GetMethod.ReturnType.Name == typeof(string).Name)
{
if (obj.GetType().GetProperty(info.Name).GetValue(obj, null) != null)
{
string plaintext = obj.GetType().GetProperty(info.Name).GetValue(obj, null).ToString();
string ciphertext = PEIS_Interface_HN.Common.AESUtils.Encrypt_Hex(plaintext, "db6d159a3b341cc53373aab7f72ccb1896b88782", "");
obj.GetType().GetProperty(info.Name).SetValue(obj, ciphertext);
}
}
else if (info.PropertyType.IsGenericType)//info.GetMethod.ReturnType.Name == typeof(List<>).Name )
{
var list = obj.GetType().GetProperty(info.Name).GetValue(obj, null) as IList;
if (list != null)
{
foreach (var item in list)
{
var ps = item.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo i in ps)
{ if (i.GetMethod.ReturnType.Name == typeof(string).Name)
{
if (item.GetType().GetProperty(i.Name).GetValue(item, null) != null)
{
string plaintext = item.GetType().GetProperty(i.Name).GetValue(item, null).ToString();
string ciphertext = PEIS_Interface_HN.Common.AESUtils.Encrypt_Hex(plaintext, "db6d159a3b341cc53373aab7f72ccb1896b88782", "");
item.GetType().GetProperty(i.Name).SetValue(item, ciphertext);
}
}
else if (i.PropertyType.IsClass&&!i.PropertyType.IsGenericType)
{
object propertyValue = i.GetValue(item);
if (propertyValue != null)
{ foreach (var it in propertyValue.GetType().GetProperties())
{
string plaintext = it.GetValue(propertyValue)==null ?string.Empty:it.GetValue(propertyValue).ToString();
string ciphertext = PEIS_Interface_HN.Common.AESUtils.Encrypt_Hex(plaintext, "db6d159a3b341cc53373aab7f72ccb1896b88782", "");
it.SetValue(propertyValue, ciphertext);
} }
}
else if (i.GetMethod.ReturnType.Name == typeof(List<>).Name)
{
var zlist = item.GetType().GetProperty(i.Name).GetValue(item, null) as IList;
if (zlist != null)
{
foreach (var it in zlist)
{
var p = it.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo zi in p)
{ if (zi.GetMethod.ReturnType.Name == typeof(string).Name)
{
if (it.GetType().GetProperty(zi.Name).GetValue(it, null) != null)
{
string plaintext = it.GetType().GetProperty(zi.Name).GetValue(it, null).ToString();
string ciphertext = PEIS_Interface_HN.Common.AESUtils.Encrypt_Hex(plaintext, "db6d159a3b341cc53373aab7f72ccb1896b88782", "");
it.GetType().GetProperty(zi.Name).SetValue(it, ciphertext);
}
}
}
}
} } } } } } }
return obj;
}
C# 处理实体类赋值(获取嵌套类型,支持list 自定义类型)的更多相关文章
- C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值
一.引入 最近遇到一个项目里面的功能,在给实体类赋值的时候,由于赋值字段是动态生成的,所以如果用常用的方法(直接实体类的名称.字段名=要赋的值),将会生成很多无用的代码,所以找到了一个通过反射的赋值与 ...
- java中遍历实体类,获取属性名和属性值
方式一(实体类): //java中遍历实体类,获取属性名和属性值 public static void testReflect(Object model) throws Exception{ for ...
- 获取jdk支持的编码类型
//获取jdk支持的编码类型 Map<String,Charset> maps = Charset.availableCharsets(); for(Map.Entry<String ...
- C#开发BIMFACE系列9 服务端API之获取应用支持的文件类型
系列目录 [已更新最新开发文章,点击查看详细] BIMFACE最核心能力之一是工程文件格式转换.无需安装插件,支持数十种工程文件格式在云端转换,完整保留原始文件信息.开发者将告别原始文件解析烦 ...
- [转]C#反射,根据反射将数据库查询数据和实体类绑定,并未实体类赋值
本文来自:http://www.cnblogs.com/mrchenzh/archive/2010/05/31/1747937.html /****************************** ...
- 好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字
在做网站没用 JS UI控件时 很实用 用法: var ps=new PageString(); /*可选参数*/ ps.SetIsEnglish = true;// 是否是英文 (默认:false) ...
- Android利用反射机制为实体类属性赋值
在做android项目时,有时会遇到从网络上获取json类型数据,赋值给实体类,实体类属性少可以一个一个的赋值,如果实体类有很多属性,赋值可能就要耗很长的功夫了,幸好Java给我们提供了反射机制.下面 ...
- MVC过滤器中获取实体类属性值
本文地址:http://www.cnblogs.com/outtamyhead/p/3616913.html,转载请保留本地址! 最近在项目遇到了这个问题:获取Action行参中实体类的属性值,主要的 ...
- hibernate映射实体类查询时数据库空字段赋值给实体类报错的问题
因为一直报实体类空异常,去网上查了资料只查到了并没有查到数据库空值时不给实体类赋值的属性 异常 org.hibernate.InvalidMappingException: Could not par ...
- iOS开发之使用Runtime给Model类赋值
本篇博客算是给网络缓存打个基础吧,本篇博客先给出简单也是最容易使用的把字典转成实体类的方法,然后在给出如何使用Runtime来给Model实体类赋值.本篇博客会介绍一部分,主要是字典的key与Mode ...
随机推荐
- 2022春每日一题:Day 7
题目:Fire 先预处理出每个F蔓延的时间,再bfs走迷宫. 代码: #include <cstdio> #include <cstdlib> #include <cst ...
- [ PHP 内核与扩展开发系列] 内存管理 —— 引用计数
对于 PHP 这种需要同时处理多个请求的程序来说,申请和释放内存的时候应该慎之又慎,一不小心便会酿成大错.另一方面,除了要安全申请和释放内存外,还应该做到内存的最小化使用,因为它可能要处理每秒钟数以千 ...
- 我的第一个项目(二):使用Vue做一个登录注册界面
好家伙, 顶不住了,太多的bug, 本来是想把背景用canvas做成动态的,但是,出现了各种问题 为了不耽误进度,我们先把一个简单的登录注册界面做出来 来看看效果: (看上去还不错) 本界面使用 ...
- C温故补缺(五):main函数的参数
main()的参数 main()函数的参数,用于在外部执行时传入参数,类似windows的bat脚本或linux的sh脚本.在bat脚本中传入参数,用%接收.sh脚本的参数用$接收. c语言编译成可执 ...
- 谈谈我的「数字文具盒」 - NextCloud
接下来两篇主要谈论 Nextcloud 和 Obsidian,因为篇幅较长,所以单出罗列出来.本文主要介绍 Nextcloud 以及使用中的技巧和心得体会. Nextcloud Nextcloud 是 ...
- async.js 版本兼容问题 async.filter举例
async3.x 和async2.6.1 版本下 const files = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt']; // Usin ...
- docker部署项目
@ 目录 前言 一.下载安装docker: 1.前提工作 1.1 查看linux版本 1.2 yum包更新到最新 1.3 安装工具包 1.4 设置yum源并更新yum包索引 2.安装docker 2. ...
- 高可用系列文章之三 - NGINX 高可用实施方案
前文链接 高可用系列文章之一 - 概述 - 东风微鸣技术博客 (ewhisper.cn) 高可用系列文章之二 - 传统分层架构技术方案 - 东风微鸣技术博客 (ewhisper.cn) 四 NGINX ...
- can not be used when making a PIE object
编译报错 relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile wi ...
- 掌握webpack(一)一张图让你明白webpack中output的filename、path、publicPath与主流插件的关系
webpack的核心概念,放到2022年相信很多的小伙伴都已经非常清楚了.但是,对于webpack配置中的output.path.output.filename以及output.publicPath, ...