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;
}

 

posted @ 2023-01-05 09:39  园友2288976  阅读(792)  评论(0编辑  收藏  举报