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 自定义类型)的更多相关文章

  1. C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值

    一.引入 最近遇到一个项目里面的功能,在给实体类赋值的时候,由于赋值字段是动态生成的,所以如果用常用的方法(直接实体类的名称.字段名=要赋的值),将会生成很多无用的代码,所以找到了一个通过反射的赋值与 ...

  2. java中遍历实体类,获取属性名和属性值

    方式一(实体类): //java中遍历实体类,获取属性名和属性值 public static void testReflect(Object model) throws Exception{ for ...

  3. 获取jdk支持的编码类型

    //获取jdk支持的编码类型 Map<String,Charset> maps = Charset.availableCharsets(); for(Map.Entry<String ...

  4. C#开发BIMFACE系列9 服务端API之获取应用支持的文件类型

    系列目录     [已更新最新开发文章,点击查看详细] BIMFACE最核心能力之一是工程文件格式转换.无需安装插件,支持数十种工程文件格式在云端转换,完整保留原始文件信息.开发者将告别原始文件解析烦 ...

  5. [转]C#反射,根据反射将数据库查询数据和实体类绑定,并未实体类赋值

    本文来自:http://www.cnblogs.com/mrchenzh/archive/2010/05/31/1747937.html /****************************** ...

  6. 好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字

    在做网站没用 JS UI控件时 很实用 用法: var ps=new PageString(); /*可选参数*/ ps.SetIsEnglish = true;// 是否是英文 (默认:false) ...

  7. Android利用反射机制为实体类属性赋值

    在做android项目时,有时会遇到从网络上获取json类型数据,赋值给实体类,实体类属性少可以一个一个的赋值,如果实体类有很多属性,赋值可能就要耗很长的功夫了,幸好Java给我们提供了反射机制.下面 ...

  8. MVC过滤器中获取实体类属性值

    本文地址:http://www.cnblogs.com/outtamyhead/p/3616913.html,转载请保留本地址! 最近在项目遇到了这个问题:获取Action行参中实体类的属性值,主要的 ...

  9. hibernate映射实体类查询时数据库空字段赋值给实体类报错的问题

    因为一直报实体类空异常,去网上查了资料只查到了并没有查到数据库空值时不给实体类赋值的属性 异常 org.hibernate.InvalidMappingException: Could not par ...

  10. iOS开发之使用Runtime给Model类赋值

    本篇博客算是给网络缓存打个基础吧,本篇博客先给出简单也是最容易使用的把字典转成实体类的方法,然后在给出如何使用Runtime来给Model实体类赋值.本篇博客会介绍一部分,主要是字典的key与Mode ...

随机推荐

  1. ubuntu20.04修改静态ip不生效问题

    一.前言 最近从头开始配置hadoop的时候,由于想切换到NAT模式下配置hadoop,但在修改ip的时候发现设置了静态ip,但ip不生效,查了很多资料,发现由于配置信息写错了. 二.解决问题 ifc ...

  2. .net 温故知新:【9】.NET日志记录 ILogger使用和原理

    日志 日志作为我们程序记录的"黑匣子"是不论什么系统都会使用到的,比如我们经常使用的log4net就是第三方日志记录提供程序.NET 支持使用各种内置和第三方日志记录提供程序的日志 ...

  3. [排序算法] 希尔排序 (C++)

    前言 本文章是建立在插入排序的基础上写的喔,如果有对插入排序还有不懂的童鞋,可以看看这里. 直接/折半插入排序 2路插入排序 希尔排序解释 希尔排序 Shell Sort 又名"缩小增量排序 ...

  4. Zabbix技术分享——使用docker-compose快速部署zabbix监控系统

    前面文章有提到过使用docker来快速拉起一个zabbix监控系统(详见:如何使用docker快速部署一个zabbix监控系统),但是要一个个执行docker启动命令去将对应的容器启动.如果要配置参数 ...

  5. Isaac SDK & Sim 环境

    Isaac 是 NVIDIA 开放的机器人平台.其 Isaac SDK 包括以下内容: Isaac Apps: 各种机器人应用示例,突出 Engine 特性或专注 GEM 功能 Isaac Engin ...

  6. 【PostgreSQL/PGSQL】创建分区表与临时表

    一.分区表 1.链接 https://blog.csdn.net/zhangyupeng0528/article/details/119423234 2.分类 列(值)分区表:partition by ...

  7. <五>模板的完全特例化和非完全特例化

    模板作为C++泛型编程的基础十分重要,其使得一份代码能用于处理多种数据类型.而有些时候,我们会希望对一些特定的数据类型执行不同的代码,这时就需要使用模板特例化(template specializat ...

  8. 玩好.NET高级调试,你也要会写点汇编

    一:背景 1. 简介 .NET 高级调试要想玩的好,看懂汇编是基本功,但看懂汇编和能写点汇编又完全是两回事,所以有时候看的多,总手痒痒想写一点,在 Windows 平台上搭建汇编环境不是那么容易,大多 ...

  9. RGB以及RGBA

    字母含义及取值 R:红色.0~255 整数 G:绿色.0~255 整数 B:蓝色.0~255 整数 A:透明度.0~1.整数或者小数 RGB和RGBA的关系 项目遇见一个需求,后台返回所占比例,前端根 ...

  10. APP兼容测试点与WEB兼容测试点

    APP兼容测试点 WEB兼容测试点