2015-07-03 61 views
3

我有一個配置文件表單,它有很多用戶選擇,並且我有點難以確定用戶輸入的內容,當通過驗證將這些值映射到對象時屬性。使用用戶輸入驗證設置字符串的大型字典

比如我有一本字典

public static Dictionary<string, string> objProfileSelections = new Dictionary<string, string>(); 

public static string MySelections(string key) 
{ 
    objProfileSelections.Add("1", "No Answer"); 
    objProfileSelections.Add("3", "Less Than $25,000"); 
    objProfileSelections.Add("5", "$35,000 to $50,000"); 
    objProfileSelections.Add("7", "$50,000 to $75,000"); 
    objProfileSelections.Add("9", "$75,000 to $100,000"); 
    objProfileSelections.Add("11", "$100,000 to $150,000"); 
    objProfileSelections.Add("13", "$150,000+"); 
    objProfileSelections.Add("2", "No Answer"); 
    objProfileSelections.Add("4", "Less Than $25,000"); 
    objProfileSelections.Add("6", "$35,000 to $50,000"); 
    objProfileSelections.Add("8", "$50,000 to $75,000"); 
    objProfileSelections.Add("10", "$75,000 to $100,000"); 
    objProfileSelections.Add("12", "$100,000 to $150,000"); 
    objProfileSelections.Add("14", "$150,000+"); 
    string item; 
    objProfileSelections.TryGetValue(key, out item); 
    return item; 
} 

我還想從用戶密鑰字符串列表來傳遞,並通過這些項目來填充的對象。問題是我不知道如何編碼它,所以它知道要去哪個屬性,我查看了反射,但我找不到任何具有映射到屬性名稱的值字典集的示例。

爲了更清楚一點,當用戶做出選擇時,它作爲參數傳遞到字典中,字典輸出項目。從關鍵1來的價值沒有答案。如果用戶選擇了所有的複選框,那麼它就是價值 - (1,3,5,7,9,11,13)。當匹配的屬性有匹配鍵時,我需要提取這些值。例如,如果用戶點擊1,5而其餘未選中,我怎麼知道用戶做了哪些選擇?我如何讓程序根據結果知道要填充哪個屬性?

*編輯 一些屬性我想它映射到

public string MyAnnualIncome{ get; set; } 
public List<string> InterestAnnualIncome{ get; set; } 

因此,第一個屬性將被取一個值,而第二個屬性將採取多個值。

當一個關鍵字匹配一個值出現在字典中時,我需要將奇數值傳遞給MyAnnualIncome並將偶數值傳遞給InterestAnnualIncome。


  • 所以沒有人是迷茫的奇數和偶數鍵設置爲目的,屬於某一屬性組和屬於另一個基於HTML的選擇甚至是那些奇數(甚至是我的選擇,奇是什麼,我很感興趣)

*更新 有沒有一種方法可以讓我有可能使用鍵像1,3,5和使用除擴展方法傳遞到一個列表。然後獲取結果並使用方法將枚舉數據類型的值轉換爲字符串?

+3

顯然,有人明白這個問題,因爲你得到了贊成。但我真的不明白你想要做什麼。 – sstan

+0

讓我試着更清楚一點,上面的字典設置爲靜態字典來驗證用戶選擇。這個html表單有一個包含這些值的複選框列表(1,2,3,4,5,6等)。所以當用戶點擊沒有答案時,不到25000美元,35000美元到50000美元,他選擇了1,3,5。如果用戶決定篡改表單和輸入(Z238),它將返回空值。但是,如果用戶正確地做了一切,我覺得正確的做法是將其映射到名爲List Annual Income的用戶屬性。 –

+0

這有幫助,謝謝。你能擴展你的文章中的代碼以包含你想要映射到的這些屬性嗎?沒有關於這些內容,定義的位置以及您希望他們返回的信息。你甚至可以發佈一些無效的代碼,如果它有助於說明你希望完成的。 – sstan

回答

0

我在這段代碼中試圖說明如何修改可能組成配置文件的不同類的屬性。修改僅通過反射來完成,即僅提供類名稱,每個類中將改變的屬性名稱以及要分配給屬性的字符串值。

不知道它適合你的期望:(

Profile profile = new Profile() ; 
profile.SetPropertyValue("hair","color","brown") ;   

internal class Profile() 
{ 
    private Hair hair_ = new Hair(); 
    private Job job_ = new Job(); 

    internal Hair hair { get { return hair_ ; } } 
    internal Job job { get { return job_ ; } } 

    private void SetPropertyValue(string profileItemName, string ItemPropertyName, string value) 
    { // it is assumed that the different items (hair or job) of the Profile are accessible 
    // with a a property 

    // first find the Item object, i.e. hair or job     
    object itemObj = this.GetType().GetProperty(profileItemName).GetValue(this,null); 
    // assign to Item property the input value, e.g. hair.color=Brown 
    itemObj.GetType().GetProperty(ItemPropertyName).SetValue(itemObj, value, null); 
    } 
} 

internal class Hair() 
{ 
    private string color_ ; 
    private string style_ ; 

    internal string color { get { return color_ ; } set {color_ = value ; } } 
    internal string style { get { return style_ ; } set {style_ = value ; } } 
} 
+0

我的期望?你花時間花了你一天的時間來幫助我一個人真棒。在我檢查出來之前,只要去吃一些腦部食物,非常感謝! –

1

希望我理解你的問題。 我想補充一個小的輔助類(這是不使用反射的解決方案,但使用委託代替):

public class PropertyModifier 
{ 
    private string text; 
    private Func<string> modifier; 

    public PropertyModifier(Func<string> modifier) 
    { 
     this.modifier = modifier; 
    } 

    public PropertyModifier With(string text) 
    { 
     PropertyModifier newModifier = new PropertyModifier(modifier); 
     newModifier.text = text; 
     return newModifier; 
    } 

    public void Modify() 
    { 
     modifier(Text); 
    } 
} 

然後我會重寫你的代碼,並有字典映射到這個類,而不是串:

public static Dictionary<string, PropertyModifier> objProfileSelections = new Dictionary<string, PropertyModifier>(); 

public static MyUserProfile Profile; //Assuming this is the object you want to modify 

public static string MySelections(string key) 
{ 
    PropertyModifier myIncome = new PropertyModifier(text => Profile.MyAnnualIncome = text); 
    PropertyModifier interestIncome = new PropertyModifier(text => Profile.InterestAnnualIncome.Add(text)); 

    objProfileSelections.Add("1", myIncome.With("No Answer")); 
    objProfileSelections.Add("3", myIncome.With("Less Than $25,000")); 
    ... 
    objProfileSelections.Add("2", interestIncome.With("No Answer")); 
    objProfileSelections.Add("4", interestIncome.With("Less Than $25,000")); 
    ... 
} 

然後,處理用戶的選擇時,從字典獲得映射PropertyModifier並調用其Modify方法。