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

Posted 健康-是最好的时光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     }

 


  
 

以上是关于2.C#中通过委托Func消除重复代码的主要内容,如果未能解决你的问题,请参考以下文章

如何在php中通过引用传递无限参数[重复]

委托代码func和Action的基本用法

在 Swift 中通过多个委托链接 UIButton 点击

使用 func<> 委托时,我可以以任何方式更改签名吗?

无法在 MySQLi 中通过引用传递参数 [重复]

vscode中通过快捷键`vh`将vue初始化代码结构自动输出