在 C# 中动态设置 class 的枚举 属性

Set enum property of class dynamically in C#

我使用的是 .net core 3.1,我有一个方法 FillData我想为我的对象随机设置(或者它可以是枚举的第一个值)枚举值,但我无法实现。我怎样才能在下面写评论行?下面的代码可以通过直接复制粘贴到 https://dotnetfiddle.net/

来运行以进行复制
using System;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        var wantedObject = MyHelper.FillData<Student>();
        Console.WriteLine(wantedObject.Gender);
    }

    public static class MyHelper
    {
        public static T FillData<T>()
        {
            Type type = typeof(T);
            PropertyInfo[] properties = type.GetProperties();
            var resultObject = (T)Activator.CreateInstance(typeof(T), new object[]{});
            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType == typeof(string))
                {
                    property.SetValue(resultObject, "asdf");
                }
                else if (property.PropertyType.BaseType.FullName == "System.Enum")
                {
                    // property.SetValue(resultObject, ???????? );
                }
            }

            return resultObject;
        }
    }

    public class Student
    {
        public string Name{get;set;}
        public string Surname{get;set;}
        public GenderEnum Gender{get;set;}
        public LevelEnum Level{get;set;}
    }

    public enum GenderEnum
    {
        Male = 1,
        Female = 2,
    }

    public enum LevelEnum
    {
        High = 1,
        Low = 2,
    }
}

获取可用值Enum.GetValues(Type),然后select随机取一个

例如

//...

Array values = Enum.GetValues(property.PropertyType);
int index = random.Next(0, values.Length - 1); //Assuming a Random class
object value = values.GetValue(index);

property.SetValue(resultObject, value);

//...

如果您想默认为默认值,那么您可以使用 [DefaultValue] 属性。

using System;
using System.Reflection;
using System.ComponentModel;

public class Program
{
    public static void Main()
    {
        var wantedObject = MyHelper.FillData<Student>();
        Console.WriteLine(wantedObject.Gender);
    }

    public static class MyHelper
    {
        public static T FillData<T>()
        {
            Type type = typeof(T);
            PropertyInfo[] properties = type.GetProperties();
            var resultObject = (T)Activator.CreateInstance(typeof(T), new object[]{});
            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType == typeof(string))
                {
                    property.SetValue(resultObject, "asdf");
                }
                else if (property.PropertyType.BaseType.FullName == "System.Enum")
                {
                   DefaultValueAttribute[] attributes = property.PropertyType.GetCustomAttributes(typeof(DefaultValueAttribute), false) as DefaultValueAttribute[];
                   if (attributes != null && attributes.Length > 0)
                        property.SetValue(resultObject, attributes[0].Value);
                    else
                    //..do something here to get a random value.
                        property.SetValue(resultObject, 0);
                }
            }

            return resultObject;
        }
    }

    public class Student
    {
        public string Name{get;set;}
        public string Surname{get;set;}
        public GenderEnum Gender{get;set;}
        public LevelEnum Level{get;set;}
    }

    [DefaultValue(Male)]
    public enum GenderEnum
    {
        Male = 1,
        Female = 2,
    }

    [DefaultValue(High)]
    public enum LevelEnum
    {
        High = 1,
        Low = 2,
    }
}