fix(tenpayv3): 修复部分嵌套类型中属性的敏感数据不能自动加密的问题

This commit is contained in:
刘宏伟 2021-12-02 01:49:15 +00:00 committed by RHQYZ
parent 032b45bb88
commit 727039ce31

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Reflection; using System.Reflection;
@ -13,6 +13,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
InnerReplacePropertyStringValue(ref obj, replacement, null); InnerReplacePropertyStringValue(ref obj, replacement, null);
} }
private static void InnerReplacePropertyStringValue<T>(ref T obj, ReplacePropertyStringValueReplacement replacement, PropertyInfo? currentProp) private static void InnerReplacePropertyStringValue<T>(ref T obj, ReplacePropertyStringValueReplacement replacement, PropertyInfo? currentProp)
{ {
if (obj == null) throw new ArgumentNullException(nameof(obj)); if (obj == null) throw new ArgumentNullException(nameof(obj));
@ -22,6 +23,45 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (!objType.IsClass) if (!objType.IsClass)
throw new NotSupportedException(); throw new NotSupportedException();
if (objType.IsArray || obj is IList || obj is IDictionary)
{
EachCollectionProperty(ref obj, objType, replacement, null);
}
else
{
foreach (var childProp in objType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!childProp.CanWrite)
continue;
Type propType = childProp.PropertyType;
if (propType == typeof(string))
{
string oldValue = (string)childProp.GetValue(obj, null)!;
string newValue = replacement(obj, childProp, oldValue);
childProp.SetValue(obj, newValue);
}
else if (propType.IsClass)
{
object? value = childProp.GetValue(obj, null);
if (value is null)
continue;
InnerReplacePropertyStringValue(ref value, replacement, childProp);
childProp.SetValue(obj, value);
}
else
{
object? value = childProp.GetValue(obj, null);
if (value is null)
continue;
EachCollectionProperty(ref value, propType, replacement, childProp);
}
}
}
}
private static void EachCollectionProperty<T>(ref T obj, Type objType, ReplacePropertyStringValueReplacement replacement, PropertyInfo? currentProp)
{
if (objType.IsArray) if (objType.IsArray)
{ {
var array = (obj as Array)!; var array = (obj as Array)!;
@ -110,35 +150,6 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
} }
} }
} }
else
{
foreach (var childProp in objType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!childProp.CanWrite)
continue;
Type propType = childProp.PropertyType;
if (propType == typeof(string))
{
object? value = childProp.GetValue(obj, null);
if (value is null)
continue;
string oldValue = (string)value!;
string newValue = replacement(obj, childProp, oldValue);
childProp.SetValue(obj, newValue);
}
else if (propType.IsClass)
{
object? value = childProp.GetValue(obj, null);
if (value is null)
continue;
InnerReplacePropertyStringValue(ref value, replacement, childProp);
childProp.SetValue(obj, value);
}
}
}
} }
} }
} }