117 lines
4.2 KiB
C#
117 lines
4.2 KiB
C#
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("<", "<").Replace(">", ">");
|
||
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));
|
||
}
|
||
}
|
||
}
|