Return FontStyle from string name

c#, fonts, parsing

Solution

You can use reflection for this:

var propertyInfo = typeof(FontStyles).GetProperty("Italic",
                                                  BindingFlags.Static |
                                                  BindingFlags.Public |
                                                  BindingFlags.IgnoreCase);
FontStyle f = (FontStyle)propertyInfo.GetValue(null, null);

Problem

I want to write a function which will return FontStyle and take string as Parameter ``` FontStyle f = function ("Italic"); // FontStyles.Italic ``` I don't want to write Switch case or if else statements to do the same. Can it be done for case insensitive strings? ``` FontStyle f = function ("italic"); FontStyle f = function ("itAlic"); ``` should return same.

Original source