using System.Collections.Generic; using System.Text.Json.Serialization; using…
- User
using System.Collections.Generic; using System.Text.Json.Serialization; using FabiusTimerApi; using FabiusTimerApi.Models;
namespace FabiusTimer.Configs;
public class StyleConfig { [JsonPropertyName("styles")] public Dictionary<string, Style> Styles { get; set; } = new() { ["normal"] = new Style { HtmlColor = "#ffffff", Command = ["css_nm", "css_normal"], ClanTag = "Normal", CustomValues = new Dictionary<string, object> { ["mycustomvalue1"] = 32, ["mycustomvalue2"] = "test", } } };
public Style? GetStyle(string styleName) { if (Styles.TryGetValue(styleName, out var style)) return style; return null; } public string GetStyleName(Style style) { if (Styles.ContainsValue(style)) { } }}
как получить StyleName из Style
- Assistant
To retrieve a style name from a
Styleobject in yourStyleConfigclass, you need to iterate through the dictionary and compare each value with the givenStyleobject. If a match is found, the corresponding key (which represents the style name) is returned. Here's an example of how you can implement theGetStyleNamemethod:public string? GetStyleName(Style style) { foreach (var kvp in Styles) { if (kvp.Value.Equals(style)) { return kvp.Key; } } return null; }Key Points:
- Iteration: Iterate through each key-value pair in the
Stylesdictionary. The keys represent style names and the values are theStyleobjects. - Comparison: Use the
Equalsmethod to check if the currentStyleobject in the iteration is equal to the inputStyleobject. - : If a match is found, return the key (style name) associated with that object.
- Iteration: Iterate through each key-value pair in the