using System; using System.ComponentModel; class Program { static void Main(string[] args) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(int)); string intString = "42"; int intValue = (int)converter.ConvertFromString(intString); Console.WriteLine(intValue); } }
using System; using System.ComponentModel; class Program { static void Main(string[] args) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(DateTime)); string dateTimeString = "2022-01-01"; DateTime dateTimeValue = (DateTime)converter.ConvertFromString(dateTimeString); Console.WriteLine(dateTimeValue); } }In this example, we retrieve a TypeConverter object for the DateTime type using the TypeDescriptor.GetConverter method. We then use the ConvertFromString method of the TypeConverter object to convert a string representation of a date to an actual DateTime object. Package Library: The TypeDescriptor class is part of the System.ComponentModel.TypeDescriptor namespace, which is included in the .Net Standard library.