초기커밋
This commit is contained in:
37
ServerBase/Meta/MetaValidation/MetaValidator.cs
Normal file
37
ServerBase/Meta/MetaValidation/MetaValidator.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user