using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using KGySoft.CoreLibraries;
public static void Main()
ConvertTo<float>(ConsoleColor.Blue);
ConvertTo<ConsoleColor>(13);
ConvertTo<Guid>("AADC78003DAB4906826EFD8B2D5CF33D");
typeof(long).RegisterConversion(typeof(IntPtr), (obj, type, culture) => new IntPtr((long)obj));
ConvertTo<bool[]>(new List<int> { 1, 0, 0, 1 });
ConvertTo<List<int>>("Blah");
ConvertTo<string>(new[] { 'h', 'e', 'l', 'l', 'o' });
ConvertTo<ReadOnlyCollection<string>>(new[] { 1.0m, 2, -1 });
ConvertTo<ArrayList>(new HashSet<int> { 1, 2, 3 });
ConvertTo<Dictionary<ConsoleColor, string>>(new Hashtable { { 1, "One" }, { "Black", 'x' } });
private static void ConvertTo<T>(object source)
Console.Write($"{source.GetType().GetName(TypeNameKind.ShortName)} => {typeof(T).GetName(TypeNameKind.ShortName)}: {AsString(source)} => ");
T result = source.Convert<T>();
Console.WriteLine(AsString(result));
Console.WriteLine(e.Message.Replace(Environment.NewLine, " "));
private static string AsString(object obj)
if (obj is DictionaryEntry de)
return $"[{de.Key}, {de.Value}]";
if (obj is not IEnumerable || obj is string)
return ((IEnumerable)obj).Cast<object>().Select(AsString).Join(", ");