I want to parse a string into a type easily, but I don’t want to write wrapper code for each type, I just want to be able to do “1234”.Parse() or the like and have it return 1234. This should work for any type that has parsing capabilities.
Share
My solution works for any type that implements the static method TryParse(string, out T), whether it’s a class or a struct. Also, it will work for nullable structs, e.g.
and since System.Net.IPAddress has TryParse,
I create the code with the System.Linq.Expressions framework, and then cache the created lambda. Since this is done in a generic static class with a specified type, this happens only once per type to parse.
pants!