This commit is contained in:
Yuan
2022-07-24 14:48:54 +08:00
parent 5907f586ce
commit baf1bae943
115 changed files with 19923 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
using Appointment.Entity.Utility;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Appointment.Entity.ValidateAttribute
{
/// <summary>
/// 验证的执行
/// </summary>
/// <remarks>
/// Add:yuangb 2019-3-5
/// </remarks>
public static class AttributeExtend
{
/// <summary>
/// 全部实体属性根据特性校验
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t">验证的实体</param>
/// <returns></returns>
public static ApiResult Validate<T>(this T t) where T : class
{
List<string> validates = new List<string>();
Type type = t.GetType();
foreach (PropertyInfo prop in type.GetProperties())
{
object oValue = prop.GetValue(t);
foreach (ValidateAttribute attr in prop.GetCustomAttributes(typeof(ValidateAttribute), true))
{
string msg = attr.Validate(oValue);
if (!string.IsNullOrWhiteSpace(msg)) validates.Add(msg);
}
// 2020-10-12 添加屏蔽HTML注入
if (prop.PropertyType.Name == "String")
{
string sValue = (string)prop.GetValue(t);
if (sValue != null && !string.IsNullOrEmpty(sValue))
{
var isIgnoreHtml = prop.GetCustomAttribute(typeof(IsIgnoreHtml), true);
if (isIgnoreHtml == null)
{
sValue = sValue.Replace("<", "&lt;").Replace(">", "&gt;");
prop.SetValue(t, sValue);
}
}
}
}
return validates.Count == 0 ? ApiResult.Success() : ApiResult.Failed(string.Join(",", validates));
}
/// <summary>
/// 指定实体的属性名称,根据特性进行校验
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t">验证的实体</param>
/// <param name="list">实体的属性名称</param>
/// <returns></returns>
public static ApiResult Validate<T>(this T t, List<string> list) where T : class
{
List<string> validates = new List<string>();
Type type = t.GetType();
foreach (PropertyInfo prop in type.GetProperties())
{
object oValue = prop.GetValue(t);
if (list.Contains(prop.Name))
{
foreach (ValidateAttribute attr in prop.GetCustomAttributes(typeof(ValidateAttribute), true))
{
string msg = attr.Validate(oValue);
if (!string.IsNullOrWhiteSpace(msg)) validates.Add(msg);
}
}
}
return validates.Count == 0 ? ApiResult.Success() : ApiResult.Failed(string.Join(",", validates));
}
/// <summary>
/// 使用委托,全部实体属性根据特性校验
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t">验证的实体</param>
/// <param name="func">一个ApiResult入参,一个ApiResult返回值的委托</param>
/// <returns></returns>
public static ApiResult Validate<T>(this T t, Func<ApiResult, ApiResult> func)
{
List<string> validates = new List<string>();
Type type = t.GetType();
foreach (PropertyInfo prop in type.GetProperties())
{
object oValue = prop.GetValue(t);
foreach (ValidateAttribute attr in prop.GetCustomAttributes(typeof(ValidateAttribute), true))
{
string msg = attr.Validate(oValue);
if (!string.IsNullOrWhiteSpace(msg))
{
validates.Add(msg);
}
}
}
return validates.Count == 0 ? func.Invoke(ApiResult.Success()) : ApiResult.Failed(string.Join(",", validates));
}
}
}