38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using System.Reflection;
|
|
|
|
|
|
using ServerCore; using ServerBase;
|
|
|
|
|
|
namespace ServerBase;
|
|
|
|
public class MetaValidator
|
|
{
|
|
public void validate<T>(IReadOnlyList<T> list, ValidatorErrorCollection errors)
|
|
{
|
|
var thisAssembly = Assembly.GetExecutingAssembly();
|
|
var methods = thisAssembly.GetTypes()
|
|
.SelectMany(x => x.GetMethods(BindingFlags.Public | BindingFlags.Static))
|
|
.Where(x => x.GetCustomAttributes<MetaValidatorAttribute>().Any())
|
|
.Where(x => x.GetParameters()?[0]?.ParameterType == typeof(T))
|
|
.ToList();
|
|
|
|
if (methods.Count == 0)
|
|
{
|
|
Log.getLogger().error($"not found validateFunc. type: {typeof(T).Name}");
|
|
return;
|
|
}
|
|
|
|
var validateFunc = MethodInfoExtentions.compile<Action<T, ValidatorErrorCollection>>(methods[0]);
|
|
|
|
errors.CurrentName = typeof(T).Name;
|
|
// var invokeParams = new object[1];
|
|
foreach (var (item, i) in list.Select((value, i) => (value, i)))
|
|
{
|
|
errors.CurrentArrayIndex = i;
|
|
validateFunc.Invoke(item!, errors);
|
|
}
|
|
}
|
|
}
|
|
|