Is it possible to have a key based value type on a key / value pair in C #?

Let me give you a simple example for a better understanding of the issue.

The library contains:

enum Keys
{
    [Description("Color select")]
    ColorSelection,

    [Description("Day select")]
    DaySelection,

    [Description("Gender select")]
    GenderSelection
}


enum Color
{
    [Description("White color")]
    White,

    [Description("Black color")]
    Black,

    [Description("Brown color")]
    Brown
}
enum Day
{
    [Description("Monday")]
    Monday,

    [Description("Tueday")]
    Tuesday,

    [Description("Wednesday")]
    Wednesday
}

enum Gender
{
    [Description("Male gender")]
    Male,

    [Description("Female gender")]
    Female            
}

From the client I want:

Dictionary<Keys, ??> values = new Dictionary<Keys, ??>();

values.Add(Keys.ColorSelection, <Auto suggest color>);
values.Add(Keys.DaySelection, <Auto suggest day>); 

I can change the library to have a list valueslike Enum, but I want the client to automatically suggest values ​​with key-based filters.

Note:
  1. I do not want Class (Model) with the property to match the type, since there are many keys available in the library, and the client will select several and provide values.
  2. I cannot make a difference how List<Values>, since some cases will have the same value descriptionas the value with a different key. For example: "Yes / No, the values ​​will be the same with different keys"

? .

+4
3

:

var dictionary = new Dictionary<Keys, int>();

, Keys .

.

0

@owen-pauling; , . , Dictionary<Keys, int>:

Dictionary<Keys, int> values = new Dictionary<Keys, int>();
values.Add(Keys.ColorSelection, Convert.ToInt32(Color.Black));
values.Add(Keys.GenderSelection, Convert.ToInt32(Gender.Male));
values.Add(Keys.DaySelection, Convert.ToInt32(Day.Wednesday));

foreach (KeyValuePair<Keys, int> kv in values)
{
    dynamic enumValue = null;

    switch (kv.Key)
    {
        case Keys.ColorSelection:
            enumValue = Enum.Parse(typeof(Color), Convert.ToString(kv.Value));
            break;

        case Keys.DaySelection:
            enumValue = Enum.Parse(typeof(Day), Convert.ToString(kv.Value));
            break;

        case Keys.GenderSelection:
            enumValue = Enum.Parse(typeof(Gender), Convert.ToString(kv.Value));
            break;
    }

    Console.WriteLine(String.Format("Enum value: {0}", enumValue));
}

/* Output:
Enum value: Black
Enum value: Male
Enum value: Wednesday
*/

, , . List<DetailSelection>:

public class DetailSelection
{
    public DetailSelection() { }

    public Type SelectionType { get; set; }
    public string Description { get; set; }
    public int EnumValue { get; set; }
}

List<DetailSelection> details = new List<DetailSelection>();
details.Add(new DetailSelection() {
    Description = "Color description",
    SelectionType = typeof(Color),
    EnumValue = Convert.ToInt32(Color.Black)
});
details.Add(new DetailSelection() {
    Description = "Day description",
    SelectionType = typeof(Day),
    EnumValue = Convert.ToInt32(Day.Wednesday)
});
details.Add(new DetailSelection() {
    Description = "Gender description",
    SelectionType = typeof(Gender),
    EnumValue = Convert.ToInt32(Gender.Male)
});

foreach (var detail in details)
{
    var enumValue = Enum.Parse(detail.SelectionType, Convert.ToString(detail.EnumValue));

    Console.WriteLine(String.Format("Enum value: {0}", enumValue));
}

/* Output:
Enum value: Black
Enum value: Wednesday
Enum value: Male
*/

, , ; .: -)

, Dictionary<TKey, TValue> ; , , , , DaySelection ..

0

, (, , ) - , :

class Properties
{
    Color? ColorSelection { get; set; }
    Day? DaySelection { get; set; }
    Gender? GenderSelection { get; set; }
}

:

Properties values = new Properties();

values.ColorSelection = < Auto suggest color >;
values.DaySelection = < Auto suggest day >;

, , , , . , : , , , . , , , , - :

class Properties
{
    private Dictionary<Type, int> values = new Dictionary<Type, int>();
    Color? ColorSelection
    {
        get
        {
            return values.TryGetValue(typeof(Color), out int i)
                ? (Color?)i
                : null;
        }
        set
        {
            values[typeof(Color)] = (int)value;
        }
    }
    ...
}

API , , . Type Keys enum, . , "" Properties, , , . intellisense, .

, T4 , .

0

Source: https://habr.com/ru/post/1683968/


All Articles