C# 如何在C中为枚举属性设置值#

C# 如何在C中为枚举属性设置值#,c#,enums,properties,set,C#,Enums,Properties,Set,我有两个字符串变量中的属性名和值。当属性类型为例如字符串时,很容易设置: prop.SetValue(P, Value, null); 但是枚举类型呢? 看看这个例子: public enum enmSex { Male, Female, Trans }; public enum enmMaritalStatus { Married, Single, Divorced, Widowed }; private class Person { pub

我有两个字符串变量中的属性名和值。当属性类型为例如字符串时,很容易设置:

prop.SetValue(P, Value, null);
但是枚举类型呢? 看看这个例子:

    public enum enmSex { Male, Female, Trans };
    public enum enmMaritalStatus { Married, Single, Divorced, Widowed };

    private class Person
    {
        public string GivenName { get; set; }
        public int Age { get; set; }
        public double Weight { get; set; }
        public enmSex Sex { get; set; }
        public enmMaritalStatus MaritalStatus { get; set; }
    }

    private List<Person> People = new List<Person>();

    private void SetPersonProperty(string GivenName, string Property, string Value)
    {
        Person P = People.Where(c => c.GivenName == GivenName).First();
        PropertyInfo prop = typeof(Person).GetProperties().Where(p => p.Name == Property).First();
        if (prop.PropertyType == typeof(double))
        {
            double d;
            if (double.TryParse(Value, out d))
                prop.SetValue(P, Math.Round(d, 3), null);
            else
                MessageBox.Show("\"" + Value + "\" is not a valid floating point number.");
        }
        else if (prop.PropertyType == typeof(int))
        {
            int i;
            if (int.TryParse(Value, out i))
                prop.SetValue(P, i, null);
            else
                MessageBox.Show("\"" + Value + "\" is not a valid 32bit integer number.");
        }
        else if (prop.PropertyType == typeof(string))
        {
            prop.SetValue(P, Value, null);
        }
        else if (prop.PropertyType.IsEnum)
        {
            prop.SetValue(P, Value, null); // Error!
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        People.Add(new Person { GivenName = "Daniel" });
        People.Add(new Person { GivenName = "Eliza" });
        People.Add(new Person { GivenName = "Angel" });
        People.Add(new Person { GivenName = "Ingrid" });

        SetPersonProperty("Daniel", "Age", "18");
        SetPersonProperty("Eliza", "Weight", "61.54442");
        SetPersonProperty("Angel", "Sex", "Female");
        SetPersonProperty("Ingrid", "MaritalStatus", "Divorced");
        SetPersonProperty("Angel", "GivenName", "Angelina");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        foreach (Person item in People)
            MessageBox.Show(item.GivenName + ", " + item.Age + ", " +
                item.Weight + ", " + item.Sex + ", " + item.MaritalStatus);
    }
public enum enmSex{男、女、跨};
公共财产{已婚、单身、离婚、丧偶};
私人阶级人士
{
公共字符串GivenName{get;set;}
公共整数{get;set;}
公共双权重{get;set;}
公共enmSex Sex{get;set;}
public enmMaritalStatus MaritalStatus{get;set;}
}
私人列表人员=新列表();
私有void SetPersonProperty(字符串给定名称、字符串属性、字符串值)
{
Person P=People.Where(c=>c.GivenName==GivenName.First();
PropertyInfo prop=typeof(Person).GetProperties().Where(p=>p.Name==Property).First();
如果(prop.PropertyType==typeof(double))
{
双d;
if(双锥巴色(值,输出d))
属性设置值(P,数学四舍五入(d,3),空);
其他的
MessageBox.Show(“\”+值+“\”不是有效的浮点数。”);
}
else if(prop.PropertyType==typeof(int))
{
int i;
if(int.TryParse(值,out i))
属性设置值(P,i,null);
其他的
MessageBox.Show(“\”+值+“\”不是有效的32位整数。”);
}
else if(prop.PropertyType==typeof(string))
{
属性设置值(P,Value,null);
}
else if(prop.PropertyType.IsEnum)
{
prop.SetValue(P,Value,null);//错误!
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
添加(新人物{GivenName=“Daniel”});
添加(新人物{givename=“Eliza”});
添加(新人物{GivenName=“Angel”});
添加(新人物{GivenName=“Ingrid”});
SetPersonProperty(“丹尼尔”、“年龄”、“18岁”);
SetPersonProperty(“Eliza”、“重量”、“61.54442”);
SetPersonProperty(“天使”、“性”、“女性”);
SetPersonProperty(“Ingrid”、“MaritalStatus”、“离婚”);
SetPersonProperty(“天使”、“吉文娜”、“安吉丽娜”);
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
foreach(人员中的人员项目)
MessageBox.Show(item.GivenName+,“+item.Age+,”+
物品。重量+”,“+物品。性别+”,“+物品。状态);
}

您需要将
值中的字符串解析为所需的枚举类型:

var enumValue = Enum.Parse(prop.PropertyType, Value);
然后将其传递给
SetValue()


您需要将
Value
中的字符串解析为所需的枚举类型:

var enumValue = Enum.Parse(prop.PropertyType, Value);
然后将其传递给
SetValue()


离婚后的婚姻状况如何?如果传入的是字符串而不是枚举,则可以使用enum.Parse。顺便问一下,为什么要使用反射,而不是像在
person.Sex=Sex.Femail中那样直接在实例上设置属性?顺便说一下,它是女性的,当然不是女性邮件。显然,您需要或类似的,例如
prop.SetValue(P,Enum.Parse(prop.PropertyType,Value),null)person.Sex=Sex.Femail中那样直接在实例上设置属性?顺便说一下,它是女性的,当然不是女性邮件。显然,您需要或类似的,例如
prop.SetValue(P,Enum.Parse(prop.PropertyType,Value),null)