40 lines
No EOL
1.2 KiB
C#
40 lines
No EOL
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using UnityEngine;
|
|
|
|
namespace Module.ProjectValidator.Editor
|
|
{
|
|
internal sealed class ValidatorList
|
|
{
|
|
private readonly Dictionary<Type, object> _validators = new();
|
|
|
|
public void Add(Type type)
|
|
{
|
|
if (type.IsInterface || type.IsAbstract)
|
|
return;
|
|
|
|
var typeValidator = type.GetInterfaces().FirstOrDefault(typeInterface => typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IAttributeValidator<>));
|
|
var attType = typeValidator?.GetGenericArguments()[0];
|
|
|
|
if (attType == null)
|
|
return;
|
|
|
|
try
|
|
{
|
|
var instance = FormatterServices.GetUninitializedObject(type);
|
|
_validators.Add(attType, instance);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
}
|
|
}
|
|
|
|
public bool TryGetValue(Type type, out object validatorInstance)
|
|
{
|
|
return _validators.TryGetValue(type, out validatorInstance);
|
|
}
|
|
}
|
|
} |