2.C#中通过委托Func消除重复代码

阅读目录
   一:重复的代码

 二:C#中通过委托Func消除重复代码

   一:重复代码
   

 1 public class Persion
 2     {
 3         public string Name { get; set; }
 4         public int Age { get; set; }
 5 
 6         public Persion GetPersionInfo()
 7         {
 8             try
 9             {
10                 Persion persion = new Persion();
11                 persion.Name = "David";
12                 persion.Age = 30;
13 
14                 return persion;
15             }
16             catch (Exception ex)
17             {
18                 return null;
19             }           
20         }
21     }
 1  public class Employee
 2     {
 3         public string Post { get; set; }
 4         public int Salary { get; set; }
 5 
 6         public Employee GetEmployeeInfo()
 7         {
 8             try
 9             {
10                 Employee employee = new Employee();
11                 employee.Post = "开发";
12                 employee.Salary = 5000;
13 
14                 return employee;
15             }
16             catch(Exception ex)
17             {
18                 return null;
19             }
20         }
21     }

  二:C#中通过委托Func消除重复代码
     
如下所示,try catch 语句只有一次了,这样就消除了GetPersionInfo方法和GetEmployeeInfo的try catch语句

 1 public class Utillity
 2     {
 3         public static T TryExecute<T>(Func<T> func, string methodName)
 4         {
 5             try
 6             {
 7                 return func();
 8             }
 9             catch (Exception ex)
10             {
11                 Console.WriteLine("MethodName:" + methodName + " Error:" + ex.Message);
12                 return default(T);
13             }
14         }
15     }
 1 public class Persion
 2     {
 3         public string Name { get; set; }
 4         public int Age { get; set; }
 5 
 6         public Persion GetPersionInfo()
 7         {
 8             return Utillity.TryExecute<Persion>(() =>
 9             {
10                 Persion persion = new Persion();
11                 persion.Name = "David";
12                 persion.Age = 30;
13 
14                 return persion;
15             }
16             , "GetPersionInfo");
17         }
18     }
 1 public class Employee
 2     {
 3         public string Post { get; set; }
 4         public int Salary { get; set; }
 5 
 6         public Employee GetEmployeeInfo()
 7         {
 8             return Utillity.TryExecute(() =>
 9             {
10                 Employee employee = new Employee();
11                 employee.Post = "开发";
12                 employee.Salary = 5000;
13 
14                 return employee;
15             }
16             , "GetEmployeeInfo");
17 
18         }
19     }
 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Persion persion1 = new Persion();
 6             Persion persion2 = persion1.GetPersionInfo();
 7 
 8             Console.WriteLine("This persion name is {0}", persion2.Name);
 9             Console.WriteLine("This persion age is {0}", persion2.Age);
10 
11             Employee employee1 = new Employee();
12             Employee employee2 = employee1.GetEmployeeInfo();
13 
14             Console.WriteLine("This employee post is {0}", employee2.Post);
15             Console.WriteLine("This employee salary is {0}", employee2.Salary);
16 
17             Console.ReadLine();
18         }
19     }


  
 

原文地址:https://www.cnblogs.com/menglin2010/p/5229030.html