lundi 24 février 2020

get members value, name and custom name from enum type with its name

i want to get a string from user and show him a list of enums member and its value. for example i have this enum

public enum exampleEnum
    {
        [MyCustomProperty(customName = "نام1")]
        member1 = 1,

        [MyCustomProperty(customName = "نام2")]
        member2 = 2,

    }

help me for create this function

public List<enumResult> GetEnumDetailWithName(string enumName)
{
 ???

 return result;// {name:'member1',value:1,customName='نام1'},{name:'member2',value:2,customName='نام2'}
} 

i have write this code but not complete in this lines get enum from all assembly and its done then get all members of enum currectly then for any member of enum add a member to result list , filling name is true but i cant fill Value and CustomName Field ...

     public List<enumDetail> GetEnumDetailWithName(string enumName)
    {
        var enumFullName = $"Bamdad.PublicEnum.Enums+{enumName}";
        var assemblyList = AppDomain.CurrentDomain.GetAssemblies();
        Type type = null;
        foreach (var assembly in assemblyList)
        {
            type = assembly.GetType(enumFullName);
            if (type == null)
                continue;
            if (type.IsEnum)
                break;
        }

        if (type == null)
            return null;


        //until this line get enum currectly
        var members = type.GetMembers(BindingFlags.Public | BindingFlags.Static).Where(q=> q?.DeclaringType?.Name == enumName).ToList();
        var properties = type.GetProperties();
        if (!members.Any()) return null;

        var result = new List<enumDetail>();

        //get members currectly
        foreach (var mem in members)
        {

            var resultItem =  new enumDetail()
            {
                Name = mem.Name, // true
                Value = 0,       // i cant get
                CustomName = "???" // i cant get
            };

            result.Add(resultItem);
        }
        return result;
    }

please help me





Aucun commentaire:

Enregistrer un commentaire