To the integer value of a specific enumerator, I've built the following small helper method:
private static int GetEnumValueByName \u0026lt;T> (string name) where T: struct
{int value = 0;
try {var enumerator =
(T) Enum.Parse (typeof (T), name, true);
value = Convert.ToInt32 (enumerator);}
catch
{/ / No enumerator found
} return value;}
In my case, I read the name of the actual enumerator from the app.config and transferring it to my helper method. If the Enum is not found with the help of the name, I get 0th I would not throw any exception, as I have defined default values and use them accordingly, if my helper method returns 0th Otherwise it would of course clean when you throw an exception and the user informed so that he knows that was stored in the configuration, a bad value.
The third parameter also enables me to control yet, whether the upper / lower case should be considered. I ignore them, as it does in my code, no matter if the user "time out" or "time out" is stored in the configuration.
0 comments:
Post a Comment