[英]Validating a large dictionary of set strings with user input
我有一個有很多用戶選擇的配置文件表單,當我將驗證映射到對象屬性時,我有點難以驗證用戶輸入的內容。
例如,我有一本字典
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。
*更新有沒有辦法可以使用像1,3,5這樣的鍵,並使用except擴展方法將其傳遞到列表中。 然后獲取結果並使用方法將枚舉數據類型的值轉換為字符串?
希望我理解你的問題。 我會添加一個小助手類(這是一個不使用反射,但使用委托代替的解決方案):
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
方法。
我嘗試在此代碼中說明如何修改可能組成配置文件的不同類的屬性。 修改僅通過反射完成,即僅提供類名,每個類中將有所不同的屬性名以及要分配給屬性的字符串值。
不確定它是否符合您的期望:(
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 ; } }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.