【问题标题】:How to set a value to an enumerate property in C#如何在 C# 中为枚举属性设置值
【发布时间】:2017-09-03 15:45:50
【问题描述】:

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

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);
    }

【问题讨论】:

  • MartialStatus.Divorced 怎么样?您传递的是字符串而不是枚举,您可以使用 Enum.Parse。顺便说一句,您为什么要使用反射,而不是像 person.Sex = Sex.Femail 那样直接在您拥有的实例上设置属性?顺便说一句,是female,当然不是femail
  • 显然你需要Enum.Parse或类似的,例如prop.SetValue(P, Enum.Parse(prop.PropertyType, Value), null);
  • 这只是我为我的问题准备的一个例子。我的代码有一个非常大的类,有几个枚举类型,把它带到这里非常复杂。如果您只是复制并粘贴此代码,您将遇到该错误。我只是不知道如何解决它。我也不能对我复杂的代码做很多更改。另外我不知道如何使用 Enum.Parse。我真的想要一个可行的解决方案。谢谢。
  • 非常感谢。你救了我!这有效

标签: c# enums properties set


【解决方案1】:

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

var enumValue = Enum.Parse(prop.PropertyType, Value);

然后传递给SetValue():

prop.SetValue(P, enumValue, null);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多