AquaDX/AquaMai/AquaMai.Config/Reflection/SystemReflectionProvider.cs

48 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Reflection;
using AquaMai.Config.Interfaces;
namespace AquaMai.Config.Reflection;
public class SystemReflectionProvider(Assembly assembly) : IReflectionProvider
{
public class ReflectionField(FieldInfo field) : IReflectionField
{
public FieldInfo UnderlyingField { get; } = field;
public string Name => UnderlyingField.Name;
public Type FieldType => UnderlyingField.FieldType;
public T GetCustomAttribute<T>() where T : Attribute => UnderlyingField.GetCustomAttribute<T>();
public object GetValue(object obj) => UnderlyingField.GetValue(obj);
public void SetValue(object obj, object value) => UnderlyingField.SetValue(obj, value);
}
public class ReflectionType(Type type) : IReflectionType
{
public Type UnderlyingType { get; } = type;
public string FullName => UnderlyingType.FullName;
public string Namespace => UnderlyingType.Namespace;
public T GetCustomAttribute<T>() where T : Attribute => UnderlyingType.GetCustomAttribute<T>();
public IReflectionField[] GetFields(BindingFlags bindingAttr) => Array.ConvertAll(UnderlyingType.GetFields(bindingAttr), f => new ReflectionField(f));
}
public Assembly UnderlyingAssembly { get; } = assembly;
public IReflectionType[] GetTypes() => Array.ConvertAll(UnderlyingAssembly.GetTypes(), t => new ReflectionType(t));
public Dictionary<string, object> GetEnum(string enumName)
{
var enumType = UnderlyingAssembly.GetType(enumName);
if (enumType == null) return null;
var enumValues = Enum.GetValues(enumType);
var enumDict = new Dictionary<string, object>();
foreach (var enumValue in enumValues)
{
enumDict.Add(enumValue.ToString(), enumValue);
}
return enumDict;
}
}