The source and target types must be one of the “base” types. ChangeType also accepts an optional IFormatProvider argument. Here's an example:

image_pdfimage_print
   
  

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
    }
}

   
     


This entry was posted in Data Types. Bookmark the permalink.