예제 #1
0
 public override void OnConfigure(IDescriptorContext context, IObjectFieldDescriptor descriptor, MemberInfo member)
 {
     descriptor.Use(next => context =>
     {
         var obj       = context.GetType().GetMethod("Parent").MakeGenericMethod(context.ObjectType.RuntimeType).Invoke(context, new object[0]);
         var resultObj = (member as MethodInfo).Invoke(obj, new object[0]);
         foreach (var proName in SensitiveFields)
         {
             var resulttType = resultObj.GetType();
             //处理泛型集合
             if (resulttType.IsGenericType)
             {
                 foreach (var resultItem in (resultObj as IList))
                 {
                     SetValue(proName, resultItem.GetType(), resultItem);
                 }
             }
             else
             {
                 SetValue(proName, resulttType, resultObj);
             }
             void SetValue(string proName, Type type, object resultObj)
             {
                 var pro = type.GetProperty(proName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                 if (pro != null && pro.PropertyType.IsAssignableFrom(typeof(string)))
                 {
                     var len = pro.GetValue(resultObj).ToString()?.Length;
                     pro.SetValue(resultObj, "".PadLeft(len.Value, '*'));
                 }
             }
         }
         context.Result = resultObj;
         return(next.Invoke(context));
     });
 }