Here you can find a generic Type conversion snippet for C# and VB.NET.

Sample C#

public static T GenericConvert<T, X>(X value) where X : IConvertible
	return (T)Convert.ChangeType(value, typeof(T));

Sample VB.NET

Public Shared Function GenericConvert(Of T, X As IConvertible)(value As X) As T
	Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
End Function

3 thought on “Generic Type Conversion snippet for C# and VB.NET”

Leave a Reply