/// <summary> /// Converts customer object to an equivalent string representation using specified format and specific formatting information. /// </summary> /// <param name="format">A format string containing formatting specifications</param> /// <param name="arg">Instance of the Customer</param> /// <param name="formatProvider">An object that supplies format information about the current instance.</param> /// <returns>The string representation of Customer object, formatted as specified by format and formatProvider</returns> /// <exception cref="FormatException">The format string is invalid</exception> /// <exception cref="ArgumentException">Object reference doesn't refer to Customer instance</exception> public string Format(string format, object arg, IFormatProvider formatProvider) { Customer customer = arg as Customer; if (customer == null) { throw new ArgumentException("Wrong argument type"); } try { string result = customer.ToString(format, formatProvider); return(result); } catch (FormatException) { switch (format.ToUpperInvariant()) { case "E": return($"Customer record: {customer.Name} Revenue: {customer.Revenue} Contact phone: {customer.ContactPhone}"); default: throw new FormatException(); } } }
/// <summary> /// /// </summary> /// <param name="format"></param> /// <param name="obj"></param> /// <param name="formatProvider"></param> /// <returns></returns> public string Format(string format, object obj, IFormatProvider formatProvider) { Customer customer = (Customer)obj; format = format.ToUpperInvariant(); if (string.IsNullOrEmpty(format)) { format = "G"; } switch (format) { case "NP": return(customer?.Name + " " + customer?.ContactPhone); case "RN": return(customer?.Revenue.ToString("C", formatProvider) + " " + customer?.Name); case "RP": return(customer?.Revenue.ToString("C", formatProvider) + " " + customer?.ContactPhone); case "PN": return(customer?.ContactPhone + " " + customer?.Name); case "PR": return(customer?.ContactPhone + " " + customer?.Revenue.ToString("C", formatProvider)); default: try { return(customer.ToString(format, formatProvider)); } catch (Exception) { throw new FormatException($"Bad format {format}. Atatata"); } } }