C# 使用用户输入验证集合字符串的大型字典

C# 使用用户输入验证集合字符串的大型字典,c#,asp.net,dictionary,C#,Asp.net,Dictionary,我有一个配置文件表单,它有很多用户选择,当传递将这些值映射到对象属性的验证时,我有一种很好的方法来验证用户输入的内容 例如,我有一本字典 public static Dictionary<string, string> objProfileSelections = new Dictionary<string, string>(); public static string MySelections(string key) { objProfileSelectio

我有一个配置文件表单,它有很多用户选择,当传递将这些值映射到对象属性的验证时,我有一种很好的方法来验证用户输入的内容

例如,我有一本字典

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;
}
publicstaticdictionary objProfileSelections=newdictionary();
公共静态字符串(字符串键)
{
添加(“1”,“无答案”);
添加(“3”,“少于25000美元”);
添加(“5”,“35000美元至50000美元”);
添加(“7”,“50000美元至75000美元”);
添加(“9”,“75000美元至100000美元”);
添加(“11”,“100000美元至150000美元”);
添加(“13”和“$150000+”);
添加(“2”,“无答案”);
添加(“4”,“少于25000美元”);
添加(“6”,“35000美元至50000美元”);
添加(“8”,“50000美元至75000美元”);
添加(“10”,“75000美元至100000美元”);
添加(“12”,“100000美元至150000美元”);
添加(“14”和“$150000+”);
字符串项;
objProfileSelections.TryGetValue(键,out项);
退货项目;
}
我想从用户那里传入一个键字符串列表,并传递这些项以填充对象。问题是,我不知道如何对其进行编码,以便它知道要转到哪个属性,我查看了反射,但我找不到任何具有映射到属性名称的一组值字典的示例

更清楚地说,当用户进行选择时,它将作为参数传递到字典中,字典将输出这些项。从键1得到的值没有答案。如果用户选中所有复选框,则其值为-(1,3,5,7,9,11,13)。当存在匹配属性的匹配键时,我需要提取这些值。例如,如果用户单击1,5,但未选中其余部分,我如何知道用户做出了哪些选择?如何让程序知道根据结果填充哪个属性

*编辑 我希望将其映射到的某些属性

public string MyAnnualIncome{ get; set; }
public List<string> InterestAnnualIncome{ get; set; }
公共字符串MyAnnualIncome{get;set;}
公共列表名称{get;set;}
所以第一个属性取一个值,第二个属性取多个值

当一个键匹配字典中的一个值时,我需要奇数值进入MyAnnualIncome,偶数值进入InterestAnualIncome


  • 因此,没有人会混淆奇数键和偶数键是为了某个目的而设置的,奇数属于某个属性组,偶数属于另一个基于html选择的属性组(偶数是我的选择,奇数是我感兴趣的)

*更新
是否有一种方法可以使用像1、3、5这样的键,并使用except扩展方法将其传递到列表中。然后获取结果并使用一种方法将枚举数据类型的值转换为字符串?

我在这段代码中试图说明如何修改可能构成概要文件的不同类的属性。修改仅通过反射完成,即仅提供类名、每个类中不同的属性名以及要分配给属性的字符串值

不确定是否符合您的期望:(


希望我能理解你的问题。 我想添加一个小的助手类(这是一个不使用反射,而是使用委托的解决方案):

公共类属性修改器
{
私有字符串文本;
私有函数修饰符;
公共属性修改器(Func修改器)
{
this.modifier=修饰符;
}
具有(字符串文本)的公共属性修改器
{
PropertyModifier newModifier=新的PropertyModifier(修改器);
newModifier.text=文本;
返回newModifier;
}
public void Modify()
{
修饰语(文本);
}
}
然后我将重写您的代码,并将字典映射到此类,而不是字符串:

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"));
    ...
}
publicstaticdictionary objProfileSelections=newdictionary();
公共静态MyUserProfile;//假设这是要修改的对象
公共静态字符串(字符串键)
{
PropertyModifier myIncome=新的PropertyModifier(text=>Profile.MyAnnualIncome=text);
PropertyModifier interestIncome=新的PropertyModifier(text=>Profile.interestAnualIncome.Add(text));
objProfileSelections.Add(“1”,myIncome.With(“无答案”);
objProfileSelections.Add(“3”,myIncome.With(“少于25000美元”);
...
objProfileSelections.Add(“2”,interestIncome.With(“No-response”);
objProfileSelections.Add(“4”,利息收入(“少于25000美元”);
...
}

然后,在处理用户的选择时,从字典中获取映射的PropertyModifier,并调用其
Modify
方法。

显然,有人理解了这个问题,因为你得到了一个投票权。但我真的不明白你想做什么。让我试着更清楚一点,上面的字典被设置为静态dic用于验证用户选择的默认值。html表单有一个带有这些值(1、2、3、4、5、6等)的复选框列表。因此,当用户单击“不回答”时,少于25000美元、35000美元到50000美元,他选择值1、3、5。如果用户决定篡改表单和输入(Z238)它将返回null。但是如果用户做的每件事都正确,我觉得正确的做法是将其映射到名为List Year Income的用户属性。这很有帮助,谢谢。你能在帖子中扩展代码以包括你想映射到的这些属性吗?没有关于这些属性是什么、它们定义在哪里、你想如何使用它们的信息em返回。如果有助于说明您希望实现的目标,您甚至可以发布一些无效代码。是否要将密钥转换为c#属性名称?一旦获得属性,请执行以下操作
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"));
    ...
}