Get dictionary name by string in C#

喜夏-厌秋 提交于 2021-01-28 06:10:34

问题


I have a class which contains 4 dictionaries.

public class FishInformation
    {
        public int FishId { get; set; }
        public Dictionary<string,int> DicGroup { get; set; }
        public Dictionary<string, int> DicEvent { get; set; }
        public Dictionary<string, int> DicAudience { get; set; }
        public Dictionary<string, int> DicType { get; set; }
    }

I want get the dictionary name by a string and add items to it. So this question suggests using System.Reflection to do so. this is the code I tried:

 FishInformation fishinfo =new Global.FishInformation
            {
                FishId = fishId,
                DicAudience =  new Dictionary<string, int>(),
                DicEvent =  new Dictionary<string, int>(),
                DicGroup =  new Dictionary<string, int>(),
                DicType =  new Dictionary<string, int>()
            };
string relatedDictionary  //It's the variable which contains the string to merge with "Dic" to get the property

fishinfo.GetType().GetProperty("Dic" + relatedDictionary).SetValue(fishinfo, myKey, myValue);

I just can figure out how to make it work!


回答1:


Why so complicated? I suggest this solution/design:

class Program
{
    static void Main(string[] args)
    {
        string dicName = "Group";
        var fishInfo = new FishInformation();
        string myKey = "myKey";
        int myValue = 1;
        fishInfo.Dictionaries[dicName][myKey] = myValue;
    }
}

public class FishInformation
{
    public FishInformation()
    {
        Dictionaries = new Dictionary<string, Dictionary<string, int>>()
        {
            { "Group", new Dictionary<string, int>() },
            { "Event", new Dictionary<string, int>() },
            { "Audience", new Dictionary<string, int>() },
            { "Type", new Dictionary<string, int>() }
        };
    }

    public int FishId { get; set; }

    public Dictionary<string, Dictionary<string, int>> Dictionaries { get; set; }

    public Dictionary<string, int> GroupDic
    {
        get { return Dictionaries["Group"]; }
    }

    // ... other dictionary getters ...
}



回答2:


Your code sets the value for the entire dictionary, rather than adding a string to an existing dictionary.

You need to call GetValue, not SetValue, cast it to IDictionary<string,int>, and add the value to it:

var dict = (IDictionary<string,int>)fishinfo
    .GetType()
    .GetProperty("Dic" + relatedDictionary)
    .GetValue(fishinfo);
dict[myKey] = myValue;

This is not the most efficient way of doing this - you could use an enum instead:

enum InfoDict {Group, Event, Audience, Type};
public class FishInformation {
    public int FishId { get; set; }
    private IDictionary<string,int>[] infos = new IDictionary<string,int>[] {
        new Dictionary<string,int>()
    ,   new Dictionary<string,int>()
    ,   new Dictionary<string,int>()
    ,   new Dictionary<string,int>()
    };
    public IDictionary<string,int> GetDictionary(InfoDict index) {
        return infos[index];
    }
}



回答3:


If I understand your question correctly, you'll probably need to call GetValue() first to retrieve the dictionary, add the item to the dictionary and lastly call SetValue() (scratch that); because you're trying to modify the dictionaries contents and not the dictionary as a whole.

Edit: SetValue() isn't necessary as you're dealing with a reference type.



来源:https://stackoverflow.com/questions/27340759/get-dictionary-name-by-string-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!