refactor(tenpayv3): 优化反射性能

This commit is contained in:
Fu Diwei
2022-06-14 10:23:28 +08:00
parent 8fa58b98a1
commit a6a4b28baf
3 changed files with 25 additions and 5 deletions

View File

@@ -23,7 +23,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
try
{
bool requireEncrypt = request.GetType().GetCustomAttributes<WechatTenpaySensitiveAttribute>(inherit: true).Any();
bool requireEncrypt = Attribute.IsDefined(request.GetType(), typeof(WechatTenpaySensitiveAttribute));
if (requireEncrypt)
{
// 遍历并加密被标记为敏感数据的字段

View File

@@ -55,7 +55,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
return response;
}
bool requireDecrypt = response.GetType().GetCustomAttributes<WechatTenpaySensitiveAttribute>(inherit: true).Any();
bool requireDecrypt = Attribute.IsDefined(response.GetType(), typeof(WechatTenpaySensitiveAttribute));
if (requireDecrypt)
{
// 遍历并解密被标记为敏感数据的字段

View File

@@ -4,9 +4,24 @@ using System.Reflection;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
{
internal static class ReflectionUtility
internal static partial class ReflectionUtility
{
public delegate (bool Modified, string NewValue) ReplacePropertyStringValueReplacementHandler(object target, PropertyInfo currentProp, string oldValue);
private static readonly Hashtable _typeProperties = new Hashtable();
private static PropertyInfo[] GetTypedProperties(Type type)
{
if (type == null) throw new ArgumentNullException(nameof(type));
string skey = type.AssemblyQualifiedName ?? type.GetHashCode().ToString();
PropertyInfo[]? properties = (PropertyInfo[]?)_typeProperties[skey];
if (properties == null)
{
properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
_typeProperties[skey] = properties;
}
return properties;
}
public static void ReplacePropertyStringValue<T>(ref T obj, ReplacePropertyStringValueReplacementHandler replacement)
{
@@ -28,7 +43,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
}
else
{
foreach (var childProp in objType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
foreach (var childProp in GetTypedProperties(objType))
{
if (!childProp.CanWrite)
continue;
@@ -188,4 +203,9 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
}
}
}
partial class ReflectionUtility
{
public delegate (bool Modified, string NewValue) ReplacePropertyStringValueReplacementHandler(object target, PropertyInfo currentProp, string oldValue);
}
}