public SettingsKey GetSettingsKeyMetadata(string name) { var field = settingsType.GetField(name); if (field == null) { throw new ArgumentException($"Settings key '{name}' is not defined"); } var attributes = field.GetCustomAttributes(); // get key definition var keyDefinitionAttribute = attributes.FirstOrDefault(ca => ca is EncodedDefinitionAttribute) as EncodedDefinitionAttribute; if (keyDefinitionAttribute == null) { throw new SettingsSynchronizationException($"Definition attribute is missing for key { field.Name }"); } var key = MetadataEncoder.Decode(keyDefinitionAttribute.Definition); // apply definition overrides var defaultValueAttribute = attributes.FirstOrDefault(ca => ca is DefaultValueAttribute) as DefaultValueAttribute; if (defaultValueAttribute != null) { key.KeyDefaultValue = defaultValueAttribute.DefaultValue; } // get key placement var categoryAttribute = attributes.FirstOrDefault(ca => ca is CategoryAttribute) as CategoryAttribute; if (categoryAttribute == null) { throw new SettingsSynchronizationException($"Category attribute is missing for key { field.Name }"); } var category = new Category(categoryAttribute.DisplayName, categoryAttribute.Name, categoryAttribute.ParentName); var groupAttribute = attributes.FirstOrDefault(ca => ca is GroupAttribute) as GroupAttribute; if (groupAttribute == null) { throw new SettingsSynchronizationException($"Group attribute is missing for key { field.Name }"); } var group = new Group(groupAttribute.DisplayName, category); key.Group = group; return(key); }
public static string GetTemplateSourceCode(SettingsKey settingsKey) { if (settingsKey == null) { throw new ArgumentNullException(nameof(settingsKey)); } var key = settingsKey; var codeLines = new[] { "/// <summary>", $"/// { key.KeyDescription }", "/// </summary>", $"[{ nameof(CategoryAttribute) }(\"{ key.Group.Category.DisplayName }\", \"{ key.Group.Category.Name }\", \"{ key.Group.Category.CategoryParentName }\")]", $"[{ nameof(GroupAttribute) }(\"{ key.Group.DisplayName }\")]", $"[{ nameof(DefaultValueAttribute) }({ (key.KeyDefaultValue == null ? "null" : $"@\"{ EscapeQuotes(key.KeyDefaultValue) }\"" ) })]", $"[{ nameof(EncodedDefinitionAttribute) }(\"{ MetadataEncoder.Encode(key) }\")]", $"public const string { key.KeyName } = \"{ key.KeyName }\";" }; var code = string.Join(Environment.NewLine, codeLines); return(code); }