using System.Reflection;
namespace XFEExtension.NetCore.AutoConfig;
///
/// 配置文件信息
///
public class ProfileInfo
{
///
/// 配置文件类型
///
public Type Profile { get; init; }
///
/// 配置文件储存位置
///
public string Path { get; init; }
///
/// 配置文件描述
///
public string? Description { get; set; }
///
/// 实例成员信息
///
public PropertyInfo? InstancePropertyInfo { get; private set; }
///
/// 配置文件属性列表
///
public List MemberInfo { get; private set; }
///
/// 配置文件信息
///
/// 配置文件类型
/// 配置文件储存位置
/// 配置文件描述
public ProfileInfo(Type profileType, string path = "", string description = "")
{
Profile = profileType;
var pathAttribute = profileType.GetCustomAttribute();
if (path == "")
{
if(pathAttribute is not null)
{
Path = pathAttribute.Path;
}
else
{
if (XFEProfile.ProfilesDefaultPath[^1] == '/' || XFEProfile.ProfilesDefaultPath[^1] == '\\')
Path = $"{XFEProfile.ProfilesDefaultPath}{profileType.Name}.xpf";
else
Path = $"{XFEProfile.ProfilesDefaultPath}/{profileType.Name}.xpf";
}
}
else
{
Path = path;
}
Description = description;
var profileEntryList = new List();
foreach (var memberInfo in profileType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
if (memberInfo is not MethodInfo)
{
if (memberInfo.GetCustomAttribute() is not null)
profileEntryList.Add(new ProfileEntryInfo(memberInfo.Name, memberInfo));
if (memberInfo.GetCustomAttribute() is not null)
InstancePropertyInfo = memberInfo as PropertyInfo;
}
MemberInfo = profileEntryList;
}
///
/// 获取当前配置文件的数据实例
///
///
public object? GetProfileInstance() => InstancePropertyInfo?.GetValue(null);
///
/// 配置文件类型生成
///
///
public static implicit operator ProfileInfo(Type profileType) => new(profileType);
}