Mega Code Archive

 
Categories / C# / Data Types
 

The source and target types must be one of the base types ChangeType also accepts an optional IFormatProvider argument Heres an

using System; public class MainClass {     public static void Main() {         Type targetType = typeof(int);         object source = "42";         object result = Convert.ChangeType(source, targetType);         Console.WriteLine(result);             // 42         Console.WriteLine(result.GetType());   // System.Int32     } }