The test eliminates the if-else code as follows:

#region eliminate eliminate remove if-else public static void TestRemoveIfElse() {console. WriteLine(“TestRemoveIfElse”); TestIfElsetestIfElse=newTestIfElse(); TestIfElse.TestOrderorder=newTestIfElse.TestOrderId=1,Name=”zhansan”; varresult=TestIfElse.RemoveIfElse(order,”json”); Console.WriteLine(“TestRemoveIfElse”); TestIfElse testIfElse = new TestIfElse(); TestIfElse.TestOrder order = new TestIfElse.TestOrder { Id = 1, Name = “zhansan” }; var result = TestIfElse.RemoveIfElse(order, “json”); Console.WriteLine(“TestRemoveIfElse”); TestIfElsetestIfElse=newTestIfElse(); TestIfElse.TestOrderorder=newTestIfElse.TestOrderId=1,Name=”zhansan”; varresult=TestIfElse.RemoveIfElse(order,”json”); Console.WriteLine(” eliminate remove if-else: {result}”); result = TestIfElse.RemoveIfElse1(order, “json”); Console.WriteLine(” eliminateremoveIf−Else: result”); result=TestIfElse.RemoveIfElse3(order,”json”); Console.WriteLine(” eliminate remove if-else: {result}”); result = TestIfElse.RemoveIfElse3(order, “json”); Console.WriteLine(” eliminateremoveIf−Else: result”); result=TestIfElse.RemoveIfElse3(order,”json”); Console.WriteLine(” eliminate remove if-else: {result}”); result = testIfElse.RemoveIfElse4(order, “json”); Console.WriteLine(” eliminateremoveIf−Else: result”); result=testIfElse.RemoveIfElse4(order,”plaintext”); Console.WriteLine(” eliminate remove if-else: {result}”); result = testIfElse.RemoveIfElse4(order, “plaintext”); Console.WriteLine(” eliminateremoveIf−Else: result”); result=testIfElse.RemoveIfElse4(order,”plaintext”); Console.WriteLine(” eliminate remove if-else: {result}”); }

Public class TestIfElse {/// <summary> The most common way / / / < / summary > / / / < param name = "order" > < param > / / / < param name = "formatType" > < param > / / / < returns > < / returns > public static string RemoveIfElse(TestOrder order, string formatType) { string result = string.Empty; if (formatType.Equals("json", StringComparison.OrdinalIgnoreCase)) { result = JsonConvert.SerializeObject(order); } else if (formatType.Equals("plaintext", StringComparison.OrdinalIgnoreCase)) { result = $"id={order.Id},name={order.Name}"; } else { result = $"Unknown format"; } return result; } /// <summary> /// the second way, assuming only two cases, /// </param name="order"></param> // <param name="formatType"></param> /// <returns></returns> public static string RemoveIfElse1(TestOrder order, String formatType) {/ / prerequisites if (string. IsNullOrWhiteSpace (formatType)) return $" Unknown format "; //if (formatType is null) // return $"Unknown format"; Return formatType == "json"? JsonConvert.SerializeObject(order) : $"id={order.Id},name={order.Name}"; } /// <summary> /// The value is Func < TestOrder, String > processing logic (corresponding) / / / < summary > / / / < param name = "order" > < param > / / / < param name = "formatType" > < param > / / / <returns></returns> public static string RemoveIfElse3(TestOrder order, String formatType) {/ / prerequisites if (string. IsNullOrWhiteSpace (formatType)) return $" Unknown format "; // The dictionary should be pre-set elsewhere and used here, Dictionary<string, Func<TestOrder, string>> dict = new Dictionary<string, Func<TestOrder, string>> { ["json"] = a => JsonConvert.SerializeObject(a), ["plaintext"] = a => $"id={a.Id},name={a.Name}" }; Return dict[formatType]? .Invoke(order); // </summary> // <param name="order"></param> // <param > // <param </param> // < RETURNS ></ RETURNS > public string RemoveIfElse4(TestOrder Order, String formatType) {/ / prerequisites if (string. IsNullOrWhiteSpace (formatType)) return $" format is null "; //// dictionaries should be pre-set elsewhere and used here, Var dict = GetType().assembly.getexportedTypes ().where (t => t.geetinterfaces ().contains (typeof(IFormat123))) .ToDictionary(t => t.GetCustomAttribute<FlagFormatAttribute>().Name); if (dict[formatType] is null) return $"No valid format"; Var format = Activator.createInstance (dict[formatType]) as IFormat123; return format.Format(order); } /// </summary> // </summary> public interface IFormat123 {/// </summary> // </summary> /// <param name="order"></param> /// <returns></returns> string Format(TestOrder order); } /// </summary> [FlagFormat(Name = "json")] public class JsonFormat: IFormat123 { public string Format(TestOrder order) { return JsonConvert.SerializeObject(order); // </summary> [FlagFormat(Name = "plaintext")] public class PlainTextFormat: IFormat123 { public string Format(TestOrder order) { return $"id={order.Id},name={order.Name}"; // </summary> [FlagFormat(Name = "XML ")] public class XMLFormat: IFormat123 { public string Format(TestOrder order) { return $"<id>{order.Id}></id>,<name>{order.Name}</name>"; }} #endregion // <summary> // </summary> public class TestOrder {public int Id {get; set; } public string Name { get; set; }} // <summary> /// custom features, // </summary> [AttributeUsage(AttributeTargets.Class)] public Class FlagFormatAttribute: Attribute {/// <summary> /// Name /// </summary> public string Name {get; set; } } } #endregionCopy the code

The test results are as follows:

Public class Program {static void Main(string[] args) {#region eliminate remove if-else TestRemoveIfElse(); #endregion Console.Read(); }}