CfgSectionAttribute.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // <auto-generated/>
  2. // 此文件由源生成器注入到用户项目中
  3. namespace Apq.Cfg;
  4. /// <summary>
  5. /// 标记一个类为配置节,源生成器将为其生成零反射的绑定代码
  6. /// </summary>
  7. /// <remarks>
  8. /// 使用示例:
  9. /// <code>
  10. /// [CfgSection("Database")]
  11. /// public partial class DatabaseConfig
  12. /// {
  13. /// public string ConnectionString { get; set; }
  14. /// public int Timeout { get; set; } = 30;
  15. /// }
  16. ///
  17. /// // 使用生成的绑定方法
  18. /// var config = DatabaseConfig.BindFrom(cfg.GetSection("Database"));
  19. /// </code>
  20. /// </remarks>
  21. [System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
  22. public sealed class CfgSectionAttribute : System.Attribute
  23. {
  24. /// <summary>
  25. /// 配置节路径,如 "Database" 或 "App:Settings"
  26. /// </summary>
  27. public string SectionPath { get; }
  28. /// <summary>
  29. /// 是否生成扩展方法,默认为 true
  30. /// </summary>
  31. public bool GenerateExtension { get; set; } = true;
  32. /// <summary>
  33. /// 创建配置节特性
  34. /// </summary>
  35. /// <param name="sectionPath">配置节路径,为空时使用类名(去掉 Config/Settings 后缀)</param>
  36. public CfgSectionAttribute(string sectionPath = "")
  37. {
  38. SectionPath = sectionPath;
  39. }
  40. }