ConfigurationPropertyAttribute.cs 1.5 KB

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