ChangeType
using System.ComponentModel; public static U ChangeType<U>(this object source, U returnValueIfException) { try { return source.ChangeType<U>(); } catch { return returnValueIfException; } } public static U ChangeType<U>(this object source) { if (source is U) return (U)source; var destinationType = typeof(U); if (destinationType.IsGenericType && destinationType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) destinationType = new NullableConverter(destinationType).UnderlyingType; return (U)Convert.ChangeType(source, destinationType); }Example:
string a = "1234"; int b = a.ChangeType<int>(); //Successful conversion to int (b=1234) string c = b.ChangeType<string>(); //Successful conversion to string (c="1234") string d = "foo"; int e = d.ChangeType<int>(); //Exception System.InvalidCastException int f = d.ChangeType(0); //Successful conversion to int (f=0)
Description
Converts any type to another.
Details
- Author: Piero Alvarez Fuentes
- Submitted on: 11/26/2012 10:23:03 AM
- Language: C#
- Type: System.Object
- Views: 4612
Double click on the code to select all.