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,反射等手段简化重构代码的过程. 代码涉 ...
随机推荐
- jquery 编码解码
中文转Unicode:HttpUtility.UrlEncodeUnicode(string str); 转换后中文格式:"%uxxxx" 举例:"柳_abc123&qu ...
- 设计模式->观察者模式
观察者模式能非常大的减少模块之前的耦合.具体的观察者模式,客官们可以去看<设计模式>或者<Head first设计模式>等之类的书. 在java中,java.util库中封装了 ...
- VPS CentOS-6 下 LNMP HTTP服务器的搭建
VPS CentOS-6 下 LNMP HTTP服务器的搭建 前言 恢复更新后的第一篇博文, 前段时间由于各种理由, 把博客更新给宕掉了, 个人独立博客的开发也搁浅了, 现在随着工作的逐步稳定, 决心 ...
- [stm32] MPU6050 HMC5883 Kalman 融合算法移植
一.卡尔曼滤波九轴融合算法stm32尝试 1.Kalman滤波文件[.h已经封装为结构体] /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronic ...
- [WinAPI] API 14 [获取、设置文件属性和时间]
>_< 为了获取文件属性,用户可以使用GetFileAttributes与GetFileAttributesEx函数. GetFileAttributesEx函数除了返回文件属性外,还返回 ...
- Jenkins Git 中文乱码问题解决
解决方法: x:\Jenkins\jenkins.xml 新增蓝色粗体标记参数(-Dfile.encoding=utf-8),然后重启Jenkins服务,完毕! <arguments>-X ...
- NodeJS系列~目录
回到占占推荐博客索引 Node.js官网对它的阐述 Node.js is a platform built on Chrome's JavaScript runtime for easily buil ...
- paip.提升效率--批量变量赋值 “多元”赋值
paip.提升效率--批量变量赋值 "多元"赋值 ##石麻是批量变量赋值. 为一组变量赋值. 例子 1 <?php $my_array = array("Dog&q ...
- spl_autoload_register装在函数的正确写法
AutoLoading\loading <?php namespace AutoLoading; class Loadind { public static function autoload( ...
- c++ string 和wstring 之间的互相转换函数
#include <string> std::string ws2s(const std::wstring& ws) { std::string curLocale = setlo ...