2014-12-07 67 views
1

我有一个包含4个字典的类。C#中的字符串获取字典名称#

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; } 
    } 

我想通过字符串获取字典名称并向其中添加项目。所以this question建议使用System.Reflection这样做。这是我试过的代码:

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); 

我只是想弄清楚如何使它工作!

回答

1

为什么这么复杂?我建议此溶液/设计:

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 ... 
} 
+0

我必须在'Dictionaries = new ...'之前加'var'吗? Cz它抛出错误没有它 – 2014-12-07 09:16:45

+0

反正,就像这个想法!谢谢 – 2014-12-07 09:32:33

+0

不,你不可以在字典之前放var,因为它是一个属性,你给它赋了一个新的值。如果你把var放在那里,它会使它变成局部变量,并且不再工作。如果你得到一个错误,那么你需要定义属性。我想你可能会监督它? – t3chb0t 2014-12-07 12:12:18

2

您的代码设置整个字典的值,而不是将字符串添加到现有字典。

你需要调用GetValue,不SetValue,将它转换为IDictionary<string,int>,并添加值到它:

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

这是不是这样做的最有效的方式 - 你可以使用一个enum代替:

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]; 
    } 
} 
+0

它将引发'对方法没有过载 '的GetValue' 需要1个arguments'误差在'.GetValue(fishinfo);' – 2014-12-07 08:24:16

+0

@AlexJolig该方法应该在那里在.NET 4.5( [DOC](http://msdn.microsoft.com/en-us/library/hh194385(v = vs.110)的.aspx))。如果你使用4.5以前的.NET,请改为使用'.GetValue(fishInfo,new object [0])'。 – dasblinkenlight 2014-12-07 08:32:09

+0

为您的解决方案投票。谢谢。但我会用@ t3chb0t答案。 – 2014-12-07 09:35:01

0

如果我正确理解你的问题,你可能需要先调用的GetValue()来检索字典,该项目添加到字典中,最后调用的SetValue()(第从零开始在);因为你试图修改字典内容而不是整个字典。

编辑:SetValue()是不必要的,因为你正在处理一个引用类型。

相关问题