mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 移除多余using语句和空行,统一代码缩进格式 - 优化注释文档中的缩进和对齐格式 - 简化条件语句和方法实现,提升代码可读性 - 重构协程系统相关类的字段和方法定义格式 - 更新架构服务中容器访问方式的实现 - 调整异步操作类的属性和方法组织结构 - [skip ci]
35 lines
1.0 KiB
C#
35 lines
1.0 KiB
C#
using System.Reflection;
|
|
|
|
namespace GFramework.Game.Abstractions.setting;
|
|
|
|
/// <summary>
|
|
/// 设置数据抽象基类,提供默认的 Reset() 实现
|
|
/// </summary>
|
|
public abstract class SettingsData : ISettingsData
|
|
{
|
|
/// <summary>
|
|
/// 重置设置为默认值
|
|
/// 使用反射将所有属性重置为它们的默认值
|
|
/// </summary>
|
|
public virtual void Reset()
|
|
{
|
|
var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
foreach (var prop in properties)
|
|
{
|
|
if (!prop.CanWrite || !prop.CanRead) continue;
|
|
|
|
var defaultValue = GetDefaultValue(prop.PropertyType);
|
|
prop.SetValue(this, defaultValue);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定类型的默认值
|
|
/// </summary>
|
|
/// <param name="type">要获取默认值的类型</param>
|
|
/// <returns>类型的默认值</returns>
|
|
private static object? GetDefaultValue(Type type)
|
|
{
|
|
return type.IsValueType ? Activator.CreateInstance(type) : null;
|
|
}
|
|
} |