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#基础
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~