C# Code Snippets

Generic String Parsing

A string can be easily converted into a generic type with the TypeDescriptor class under the System.ComponentModel namespace.

Platform: .NET Framework 4.0

public static T As<T>(this string input)
{
    return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(input);
}

http://stackoverflow.com/questions/2961656/generic-tryparse

 

Back to C# Code Snippet List

Other C# Articles
Unable to connect to database! Please try again later.