2.C#中通过委托Func消除重复代码
阅读目录
一:重复的代码
二:C#中通过委托Func消除重复代码
一:重复代码
public class Persion
{
public string Name { get; set; }
public int Age { get; set; } public Persion GetPersionInfo()
{
try
{
Persion persion = new Persion();
persion.Name = "David";
persion.Age = ; return persion;
}
catch (Exception ex)
{
return null;
}
}
}
public class Employee
{
public string Post { get; set; }
public int Salary { get; set; } public Employee GetEmployeeInfo()
{
try
{
Employee employee = new Employee();
employee.Post = "开发";
employee.Salary = ; return employee;
}
catch(Exception ex)
{
return null;
}
}
}
二:C#中通过委托Func消除重复代码
如下所示,try catch 语句只有一次了,这样就消除了GetPersionInfo方法和GetEmployeeInfo的try catch语句
public class Utillity
{
public static T TryExecute<T>(Func<T> func, string methodName)
{
try
{
return func();
}
catch (Exception ex)
{
Console.WriteLine("MethodName:" + methodName + " Error:" + ex.Message);
return default(T);
}
}
}
public class Persion
{
public string Name { get; set; }
public int Age { get; set; } public Persion GetPersionInfo()
{
return Utillity.TryExecute<Persion>(() =>
{
Persion persion = new Persion();
persion.Name = "David";
persion.Age = ; return persion;
}
, "GetPersionInfo");
}
}
public class Employee
{
public string Post { get; set; }
public int Salary { get; set; } public Employee GetEmployeeInfo()
{
return Utillity.TryExecute(() =>
{
Employee employee = new Employee();
employee.Post = "开发";
employee.Salary = ; return employee;
}
, "GetEmployeeInfo"); }
}
class Program
{
static void Main(string[] args)
{
Persion persion1 = new Persion();
Persion persion2 = persion1.GetPersionInfo(); Console.WriteLine("This persion name is {0}", persion2.Name);
Console.WriteLine("This persion age is {0}", persion2.Age); Employee employee1 = new Employee();
Employee employee2 = employee1.GetEmployeeInfo(); Console.WriteLine("This employee post is {0}", employee2.Post);
Console.WriteLine("This employee salary is {0}", employee2.Salary); Console.ReadLine();
}
}
2.C#中通过委托Func消除重复代码的更多相关文章
- 1.C#中通过委托Action消除重复代码
阅读目录 一:重复的代码 二:使用委托消除重复代码 一:重复的代码 我们在写一些方法的时候,会在里面可能出现异常的地方使用try catch语句,这样每个方法都会有try catch语 ...
- 使用#include消除重复代码
消除重复代码代码很多种,比如: 1)提炼成函数复用 2)使用宏 3)继承 4)使用闭包(boost::bind.boost::function) 上述是最为常用的,对于C++程序,闭包可能用得相对少一 ...
- c#中,委托Func的简单实践
c# 委托Func的简单实践最近才真正的接触委托,所以针对Func类型的委托,做一个实践练习. 首先说一些我对委托的初级理解:"就是把方法当做参数,传进委托方法里". 我平时用到的 ...
- Metalama简介2.利用Aspect在编译时进行消除重复代码
上文介绍到Aspect是Metalama的核心概念,它本质上是一个编译时的AOP切片.下面我们就来系统说明一下Metalama中的Aspect. Metalama简介1. 不止是一个.NET跨平台的编 ...
- 【译】利用Lombok消除重复代码
当你在写Getter和Setter时,一定无数次的想过,为什么会有POJO这么烂的东西.你不是一个人!(不是骂人-)无数的开发人员花费了大量的时间来写这种样板代码,而他们本来可以利用这些时间做出更有价 ...
- Android 勤用RXJava compose操作符消除重复代码
相信小伙伴在使用RXJava与Retrofit请求网络时,都有遇到过这样的场景,在IO线程请求网络解析数据,接着返回主线程setData.更新View试图,那么也肯定熟悉下面这几句代码: .subsc ...
- 代码的坏味道(14)——重复代码(Duplicate Code)
坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...
- C#中常见的委托(Func委托、Action委托、Predicate委托)
今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...
- 使用枚举类Enum作为callee和caller的约定,运用反射消除分支和重复代码在命令式程序中的应用
在开发过程中,程序提供的功能由简单变得复杂,承担功能的主要类也会因此变得庞大臃肿,如果不加以维护,就会散发出浓重的代码味道.下面这篇博文,主要讲述了利用Enum,反射等手段简化重构代码的过程. 代码涉 ...
随机推荐
- iOS中数字的格式化 NSNumberFormatter
NSNumberFormatter 和NSDateFormatter 是NsFormatter的子类. NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值 ...
- Swift闭包概念与常见使用场景总结
·Swift 闭包 闭包(Closures)是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. Swift 中的闭包与 C 和 Objective-C 中的代码块(blocks)以及其他一些 ...
- JWT【JSON Web Token】 简述
draft: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html http://tools.ietf.org/html/ ...
- Nginx学习笔记(八) Nginx进程启动分析
Nginx进程启动分析 worker子进程的执行循环的函数是ngx_worker_process_cycle (src/os/unix/ngx_process_cycle.c). 其中,捕获事件.分发 ...
- ASP.NET 5系列教程 (三):view components介绍
在ASP.NET MVC 6中,view components (VCs) 功能类似于虚拟视图,但是功能更加强大. VCs兼顾了视图和控制器的优点,你可以把VCs 看作一个Mini 控制器.它负责控制 ...
- paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结
paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结 #两个思路 1.思路如下:使用file_get_contents()获取txt文件的内容,然后通过 ...
- Leetcode 278 First Bad Version 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- Android onTouch事件传递机制
Android onTouch事件介绍: Android的触摸事件:onClick, onScroll, onFling等等,都是由许多个Touch组成的.其中Touch的第一个状态肯定是ACTION ...
- NGUI ScrollView 循环 Item 实现性能优化
今天来说说一直都让我在项目中头疼的其中一个问题,NGUI 的scrollView 列表性能问题,实现循环使用item减少性能上的开销. 希望能够给其他同学们使用和提供一个我个人的思路,这个写的不是太完 ...
- 写hive db的两种方法
方法1tmp_channel_hive_file="/tmp/tmp_channel_hive_file"tmp_channel_hive_file_new="/tmp/ ...