Reputation: 581
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.
Upvotes: 5
Views: 7570
Reputation: 101
In C# it is just an enumeration. So you can convert it like this:
FontStyle f = (FontStyle)Enum.Parse(typeof(FontStyle), "Italic", true);
Upvotes: 10
Reputation: 174299
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);
Upvotes: 7