zoukankan      html  css  js  c++  java
  • 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     }


      
     

  • 相关阅读:
    JTree单击事件
    hibernate、easyui、struts2整合
    ubuntu中wifi显示被硬件禁用的解决方法
    idea导入svn项目
    Intellij IDEA常用配置详解
    HBase 写优化之 BulkLoad 实现数据快速入库
    Spark性能优化之道——解决Spark数据倾斜(Data Skew)的N种姿势
    avro序列化详细操作
    wordcount代码实现详解
    idea配置maven
  • 原文地址:https://www.cnblogs.com/menglin2010/p/5229030.html
Copyright © 2011-2022 走看看