2014-12-07 53 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()是不必要的,因爲你正在處理一個引用類型。

相關問題