C
C#3y ago
Connor

How to convert this method into a generic one?

private List<string> GetEnumNamesCorrected()
{
var correctedNames = new List<string>();
foreach (var enumName in Enum.GetNames(typeof(ManagerType)))
{
var wordsInType =
Regex.Matches(enumName.ToString(), @"([A-Z][a-z]+)")
.Cast<Match>()
.Select(m => m.Value);

var withSpaces = string.Join(" ", wordsInType);
correctedNames.Add(withSpaces);
}
return correctedNames;
} // Outpets new() { "Human Resources", "General Manager" } just converts to string and adds space at Capital letters
public enum ManagerType
{
HumanResources,
GeneralManager
}
private List<string> GetEnumNamesCorrected()
{
var correctedNames = new List<string>();
foreach (var enumName in Enum.GetNames(typeof(ManagerType)))
{
var wordsInType =
Regex.Matches(enumName.ToString(), @"([A-Z][a-z]+)")
.Cast<Match>()
.Select(m => m.Value);

var withSpaces = string.Join(" ", wordsInType);
correctedNames.Add(withSpaces);
}
return correctedNames;
} // Outpets new() { "Human Resources", "General Manager" } just converts to string and adds space at Capital letters
public enum ManagerType
{
HumanResources,
GeneralManager
}
I want to be able to do ManagerType.GetEnumNamesCorrected(), as well as with all my other enums. I've come up with this sudo code but it doesn't work
public static class ExtensionMethods
{
public static List<string> GetEnumNamesCorrected<T>(this T enu) where T : Enum
{
var correctedNames = new List<string>();
foreach (var enumName in Enum.GetNames(typeof(enu))) // this is an error
{
var wordsInType =
Regex.Matches(enumName.ToString(), @"([A-Z][a-z]+)")
.Cast<Match>()
.Select(m => m.Value);

var withSpaces = string.Join(" ", wordsInType);
correctedNames.Add(withSpaces);
}
return correctedNames;
}
}
public static class ExtensionMethods
{
public static List<string> GetEnumNamesCorrected<T>(this T enu) where T : Enum
{
var correctedNames = new List<string>();
foreach (var enumName in Enum.GetNames(typeof(enu))) // this is an error
{
var wordsInType =
Regex.Matches(enumName.ToString(), @"([A-Z][a-z]+)")
.Cast<Match>()
.Select(m => m.Value);

var withSpaces = string.Join(" ", wordsInType);
correctedNames.Add(withSpaces);
}
return correctedNames;
}
}
14 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Connor
ConnorOP3y ago
This works, thanks. Do you know if it's possible to turn it into an extension method so I can do ManagerType.GetValues(); instead of GetValues<ManagerType>()?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Connor
ConnorOP3y ago
Then the extension method will be able to the values of the enum and not the enum itself
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Connor
ConnorOP3y ago
var test = ManagerType.GeneralManager;
test.GetValues();
var test = ManagerType.GeneralManager;
test.GetValues();
Thats how it works rn
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Connor
ConnorOP3y ago
I want to be able to do ManagerType.GetValues(); Using your example
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Connor
ConnorOP3y ago
Thats what Im thinking
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Connor
ConnorOP3y ago
Its fine, just semantics. ExtensionMethods.GetValues<ManagerType>(); works just as good as ManagerType.GetValues();
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Connor
ConnorOP3y ago
Thanks for the help any ways. Works perfect
Want results from more Discord servers?
Add your server