Exemple #1
0
 public ConfigurationEntryAttribute(string key, ConfigurationEntryScope scope, [CallerLineNumber] int order = 0, bool setDefaultValueIfNeeded = true) // the default order is the order of declaration in a configuration class
 {
     Key   = key;
     Order = order;
     SetDefaultValueIfNeeded = setDefaultValueIfNeeded;
     Scope = scope;
 }
        public ConfigurationEntryAttribute(string key, ConfigurationEntryScope scope, [CallerLineNumber] int order = 0, bool setDefaultValueIfNeeded = true, bool isSecured = false) // the default order is the order of declaration in a configuration class
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

#if DEBUG
            var trimmedKey = key.Trim();
            if (key != trimmedKey)
            {
                throw new InvalidOperationException($"Configuration Key '{key}' contains white-space characters.");
            }
#endif

            Key   = key;
            Order = order;
            SetDefaultValueIfNeeded = setDefaultValueIfNeeded;
            IsSecured = isSecured;
            Scope     = scope;
        }
Exemple #3
0
        public ConfigurationEntryMetadata(PropertyInfo configurationCategoryProperty, PropertyInfo configurationProperty)
        {
            if (configurationCategoryProperty is null)
            {
                throw new ArgumentNullException(nameof(configurationCategoryProperty));
            }
            if (configurationProperty is null)
            {
                throw new ArgumentNullException(nameof(configurationProperty));
            }

            var keys = new List <string>();

            foreach (var configurationEntry in configurationProperty.GetCustomAttributes <ConfigurationEntryAttribute>(inherit: true).OrderBy(x => x.Order))
            {
                keys.Add(configurationEntry.Key);

                Scope     = configurationEntry.Scope;
                IsSecured = configurationEntry.IsSecured;
            }

            if (keys.Count == 0)
            {
                throw new InvalidOperationException($"Property '{configurationProperty.Name}' does not have any configuration entry attributes.");
            }

            Keys         = keys.ToArray();
            Description  = configurationProperty.GetCustomAttribute <DescriptionAttribute>(inherit: true)?.Description;
            Type         = GetConfigurationEntryType(configurationProperty);
            DefaultValue = GetDefaultValue(configurationCategoryProperty, configurationProperty, out IsDefaultValueDynamic);

            var categoryType = configurationCategoryProperty.PropertyType.GetCustomAttribute <ConfigurationCategoryAttribute>(inherit: true);

            if (categoryType == null)
            {
                throw new InvalidOperationException($"Category '{configurationCategoryProperty.PropertyType.Name}' does not have any configuration category attributes.");
            }

            Category = categoryType.Type.GetDescription();
        }