Created
June 26, 2014 01:19
-
-
Save javafun/997b50be5e2b72574842 to your computer and use it in GitHub Desktop.
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 static T GetValue<T>(this NameValueCollection collection, string key) | |
{ | |
if(collection == null) | |
{ | |
throw new ArgumentNullException("collection"); | |
} | |
var value = collection[key]; | |
if(value == null) | |
{ | |
throw new ArgumentOutOfRangeException("key"); | |
} | |
var converter = TypeDescriptor.GetConverter(typeof(T)); | |
if(!converter.CanConvertFrom(typeof(string))) | |
{ | |
throw new ArgumentException(String.Format("Cannot convert '{0}' to {1}", value, typeof(T))); | |
} | |
return (T) converter.ConvertFrom(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment