I need to create an instance of the Enum class that does not matter 0.
Assuming an enumeration as follows:
public enum ScheduleStateEnum { Draft = 1, Published = 2, Archived = 3 }
you can create an instance like this:
ScheduleStateEnum myState = 0;
If you cannot declare a type variable and you need to access the type as a string (as in your example), use Activator.CreateInstance :
var myState = Activator.CreateInstance(Type.GetType( "Edu3.DTOModel.Schedule.ScheduleStateEnum"));
Of course, both of these options will provide you with an instance that actually has an integer value of 0 , even if the enumeration does not declare it. If you want it to be one of the values you actually declared by default, you need to use Reflection to find it, for example:
var myState = Type.GetType("Edu3.DTOModel.Schedule.ScheduleStateEnum") .GetFields(BindingFlags.Static | BindingFlags.Public) .First() .GetValue(null);
This will crash for listings that have no meaning at all. Use FirstOrDefault and check for null if you want to prevent this.
Timwi source share