Convert MemberInfo [] to Enum

I searched for a while, but could not find a solution. I have an assembly in the GAC. I have to load it using reflection. After that I need to turn and turn to Ennum. But instead, I can just get it MemberInfo[]. I do not understand how to convert MemberInfo[]to Enum.

I have a code like this:

//test assembly contains 
public class MyClass
{
    public enum MyEnum 
    {
        MyVavue, 
        MyValue2
    }
}

Assembly s = Assembly.Load("test");
Type type = s.GetTypes()[1];
MemberInfo[] memberInfos = type.GetMembers(
    BindingFlags.Public | 
    BindingFlags.Static);

//I need to convert memberInfos to MyEnum
//and after that receive ---> MyEnum.MyValue <---  
+5
source share
3 answers

You should just use it Enum.GetValues. This is exactly what it does - use reflection to get enum fields:

Assembly s = Assembly.Load("test");
Type type = s.GetTypes()[1];
object[] values = Enum.GetValues(type);
object myValue = values.First(v => v.ToString() == "MyValue");
+5
source

Use GetFieldsinstead GetMembers, and then call GetValue(null)to get the value of the enumeration.

+2
source

you can use

foreach(var member in memberinfos)
{
 Enum.Parse(typeof(MyEnum),member.Name)
}


I havent tried yet .. but more or less the syntax will be the same

+1
source

All Articles