using System.Reflection;
namespace XFEExtension.NetCore.WinUIHelper.Utilities.Helper;
///
/// 配置文件帮助类
///
public static class ProfileHelper
{
///
/// 设置配置文件值
///
/// 配置文件属性路径
/// 属性值
///
public static void SetProfileValue(string profilePath, object? value)
{
var splitPath = profilePath.Split('.');
if (splitPath.Length < 2)
throw new ArgumentException("Invalid property path. It should be in the format 'NameSpace.ClassName.PropertyName'.");
string[] className = splitPath[..^1];
var propertyName = splitPath[^1];
var type = Assembly.GetEntryAssembly()?.GetType(string.Join(".", className)) ?? throw new ArgumentException($"Type '{className}' not found.");
var propertyInfo = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public) ?? throw new ArgumentException($"Property '{propertyName}' not found on type '{className}'.");
if (value is not null && !propertyInfo.PropertyType.IsAssignableFrom(value.GetType()))
throw new ArgumentException($"Value is not assignable to property '{propertyName}' of type '{propertyInfo.PropertyType}'.");
propertyInfo.SetValue(null, value);
}
///
/// 获取配置文件值
///
/// 配置文件属性路径
///
public static object? GetProfileValue(string profilePath)
{
var splitPath = profilePath.Split('.');
if (splitPath.Length < 2)
return default;
string[] className = splitPath[..^1];
var propertyName = splitPath[^1];
var type = Assembly.GetEntryAssembly()?.GetType(string.Join(".", className));
if (type is null)
return default;
var propertyInfo = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public);
if (propertyInfo is null)
return default;
return propertyInfo.GetValue(null);
}
///
/// 获取配置文件值
///
/// 目标属性类型
/// 配置文件属性路径
///
public static T? GetProfileValue(string profilePath)
{
var splitPath = profilePath.Split('.');
if (splitPath.Length < 2)
return default;
string[] className = splitPath[..^1];
var propertyName = splitPath[^1];
var type = Assembly.GetEntryAssembly()?.GetType(string.Join(".", className));
if (type is null)
return default;
var propertyInfo = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public);
if (propertyInfo is null || !propertyInfo.PropertyType.IsAssignableFrom(typeof(T)))
return default;
var result = propertyInfo.GetValue(null);
if (result is T resultT)
return resultT;
return default;
}
///
/// 获取枚举类型的配置文件项的保存方法
///
/// 目标配置文件项泛型
/// 保存方法
public static Func GetEnumProfileSaveFunc() where T : struct => value => Enum.Parse(value);
///
/// 获取ComboBox的枚举类型的配置文件项的加载方法
///
/// ComboBox的枚举类型加载方法
public static Func, object?, object?> GetEnumProfileLoadFuncForComboBox() => (items, value) => items.OfType().Where(item => item.Tag.ToString() == value?.ToString()).FirstOrDefault();
}