ConfigurationPropertyAttribute.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #nullable enable
  2. namespace Terminal.Gui.Configuration;
  3. /// <summary>An attribute indicating a property is managed by <see cref="ConfigurationManager"/>.</summary>
  4. /// <example>
  5. /// [ConfigurationProperty(Scope = typeof(AppSettingsScope))] public static string? MyProperty { get; set; } = "MyValue";
  6. /// </example>
  7. [AttributeUsage (AttributeTargets.Property)]
  8. public class ConfigurationPropertyAttribute : System.Attribute
  9. {
  10. /// <summary>
  11. /// If <see langword="true"/>, the property will be serialized to the configuration file using only the property
  12. /// name as the key. If <see langword="false"/>, the property will be serialized to the configuration file using the
  13. /// property name pre-pended with the classname (e.g. <c>Application.UseSystemConsole</c>).
  14. /// </summary>
  15. public bool OmitClassName { get; set; }
  16. private Type? _scope;
  17. /// <summary>Specifies the scope of the property. If <see langword="null"/> then <see cref="AppSettingsScope"/> will be used.</summary>
  18. public Type? Scope
  19. {
  20. get
  21. {
  22. if (_scope is { })
  23. {
  24. return _scope;
  25. }
  26. return typeof (AppSettingsScope);
  27. }
  28. set
  29. {
  30. if (value == typeof (AppSettingsScope) && OmitClassName)
  31. {
  32. throw new ArgumentException ("OmitClassName is not allowed when Scope is AppSettingsScope to ensure property names are globally unique.");
  33. }
  34. _scope = value;
  35. }
  36. }
  37. }