Created
December 10, 2014 18:52
-
-
Save bronumski/5e80b90e0b81346d733c to your computer and use it in GitHub Desktop.
Basic idea on how to look up a type converter based on a generic type and convert to that value from a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public TValue Value<TValue>(string value, TValue defaultValue = default(TValue)) | |
{ | |
if (string.IsNullOrEmpty(value)) return defaultValue; | |
var converter = TypeDescriptor.GetConverter(typeof (TValue)); | |
return (TValue) converter.ConvertFrom(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment