namespace System { public static class ConvertUtils { public static object try_convert(Type type, object obj) { try_convert(type, obj, out object result); return result; } public static bool try_convert(Type type, object obj, out object result) { if (obj == null) { result = get_default(type); return false; } else if (type.IsPrimitive && obj is IConvertible) { result = Convert.ChangeType(obj, type); return true; } else if (type.IsAssignableFrom(obj.GetType())) { result = obj; return true; } else if (type.IsEnum && obj is string) { result = try_parse_enum(type, obj.ToString()); return true; } else if (type.IsNullable()) { return try_convert(type.GetGenericArguments()[0], obj, out result); } else { result = get_default(type); return false; } } public static T try_convert(object obj) => (T)try_convert(typeof(T), obj); public static T try_convert(object obj, T default_value) => try_convert(obj, out T result) ? result : default_value; public static bool try_convert(object obj, out T result) { bool rv = try_convert(typeof(T), obj, out object r_obj); result = (T)r_obj; return rv; } public static object get_default(Type type) => type.IsValueType ? Activator.CreateInstance(type) : null; public static T ValueOrDefault(T? nullable) where T : struct => nullable.HasValue ? nullable.Value : default; public static bool is_compatible(object obj) { return obj is T || (obj == null && default(T) == null); } public static bool try_convert_args(object[] args, out T1 arg1, out T2 arg2) { if (args == null || args.Length != 2) { arg1 = default; arg2 = default; return false; } bool rv = try_convert(args[0], out arg1); rv |= try_convert(args[1], out arg2); return rv; } public static bool try_convert_args(object[] args, out T1 arg1, out T2 arg2, out T3 arg3) { if (args == null || args.Length != 3) { arg1 = default; arg2 = default; arg3 = default; return false; } bool rv = try_convert(args[0], out arg1); rv |= try_convert(args[1], out arg2); rv |= try_convert(args[2], out arg3); return rv; } public static bool try_convert_args(object[] args, out T1 arg1, out T2 arg2, out T3 arg3, out T4 arg4) { if (args == null || args.Length != 4) { arg1 = default; arg2 = default; arg3 = default; arg4 = default; return false; } bool rv = try_convert(args[0], out arg1); rv |= try_convert(args[1], out arg2); rv |= try_convert(args[2], out arg3); rv |= try_convert(args[3], out arg4); return rv; } public static object try_parse_enum(Type enum_type, string value, bool ignore_case = true) { if (!enum_type.IsEnum) throw new ArgumentException("Must an enum type.", nameof(enum_type)); Array vals = Enum.GetValues(enum_type); var str_comp = ignore_case ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; foreach (object v in vals) { if (string.Equals(v.ToString(), value, str_comp)) return v; } return Activator.CreateInstance(enum_type); } public static bool update_value_type(ref T field, T newval) where T : struct, IEquatable { bool rv = field.Equals(newval); field = newval; return rv; } public static class CastDelegate { public static readonly Func value = v => v is TResult r ? r : throw new InvalidCastException($"Failed to cast element in source collection from type {typeof(T)} to {typeof(TResult)}."); } } }