I am parsing some enum values from a text file. In order to simplify things I am using functions like the following:
(The sample code here uses C++/CLI but answers in C# are also welcome.)
bool TryParseFontStyle(String ^string, FontStyle% style){
try {
FontStyle ^refStyle = dynamic_cast<FontStyle^>(
Enum::Parse(FontStyle::typeid, string));
if(refStyle == nullptr)
return false;
style = *refStyle;
return true;
}
catch(Exception ^e){
return false;
}
}
Now I need to rewrite similar functions for each enum type that I am parsing. How do I use generics to write one single function to handle any enum type?
Update: Found a similar question here: How to TryParse for Enum value?