Is there a clever way to get an enum value by comparing a substring a la String.Contains
with the enum?
I checked this implementation (Convert a string to an enum in C#), but would like to have an intelligent extension doing the same but including substrings comparison, something like TryParse
with an option to check the whole substring of the enum values.
public static void Main()
{
string a = "HANS";
GetName(a); // prints Hans
string a1 = "Peter";
GetName(a1); // should print enum value "WolfgangPeterDietrich " containing the substring "Peter"
}
public enum MyNames
{
Hans = 0,
WolfgangPeterDietrich = 3,
}
public static void GetName(string a)
{
MyNames dif;
// this works fine, ignore case
if (Enum.TryParse(a, true, out dif))
{
System.Console.WriteLine(dif.ToString());
}
// ToDo: check for substring ??
}