Skip to content

Instantly share code, notes, and snippets.

@bronumski
Created December 10, 2014 18:52
Show Gist options
  • Save bronumski/5e80b90e0b81346d733c to your computer and use it in GitHub Desktop.
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
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