C#利用lambda表达式将函数作为参数或属性跨类传递
在编码时,由于开始是在winform下进行简单的测试开发的,后来代码多了,就想分到不同的类里边去,可是因为原来的测试是在同一个form下的,所以对于函数调用可以很方便,而一旦跨类之后,就会发现,这函数的耦合度太高,以至于不知道该怎么样解耦到类里边去。这时,不妨使用委托类型的Func和Action来实现。
下面是最开始测试时在winform里写的简单代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms; namespace FunctionPass
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
} private void buttonTest_Click(object sender, EventArgs e)
{
test();
} public void test()
{
string sourceStr = collectData();
string digitStr = fetchDigit(sourceStr);
MessageBox.Show("source:" + sourceStr + "\r\n digit:" + digitStr);
} public string collectData()
{
return Guid.NewGuid().ToString();
} public string fetchDigit(string sourceStr)
{
Regex regex = new Regex(@"\d*");
MatchCollection digitCollection=regex.Matches(sourceStr);
string digitStr = "";
foreach (Match digitMatch in digitCollection)
{
digitStr += digitMatch;
}
return digitStr;
}
}
}
这里通过colloectData函数收集数据,再通过fetchDigit提取数据中的数字,之后通过test函数显示结果。
现在我想把显示结果做成一个单独的类(比如Test类),以便后面更好的扩展。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace FunctionPass
{
public class Test
{
public void test()
{
string sourceStr = collectData();
string digitStr = fetchDigit(sourceStr);
MessageBox.Show("source:" + sourceStr + "\r\n digit:" + digitStr);
}
}
}
这时会发现,collectData和fetchDigit没有定义,那要怎么办呢?我们可以通过一个委托类型的属性来传递。下面修改后的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace FunctionPass
{
public class Test
{
public Func<string> collectData { get; set; }
public Func<string,string> fetchDigit { get; set; }
public void test()
{
string sourceStr = collectData();
string digitStr = fetchDigit(sourceStr);
MessageBox.Show("source:" + sourceStr + "\r\n digit:" + digitStr);
}
}
}
那我们又要怎么样把form中的collectData和fetchDigit函数传进去呢?可以在form像给属性赋值一样直接赋值。代码如下
private Test test_ = new Test();//为了传递和调用需要新建一个实例
private void buttonTest_Click(object sender, EventArgs e)
{
test_.collectData = collectData;//赋值
test_.fetchDigit = fetchDigit;//赋值
test_.test();
}
现在由于需要,又想把collectData和fetchDigit函数从form中独立到一个类里面,于是建了一个类,比如叫DataDeal。代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace FunctionPass
{
public class DataDeal
{
public string collectData()
{
return Guid.NewGuid().ToString();
} public string fetchDigit(string sourceStr)
{
Regex regex = new Regex(@"\d*");
MatchCollection digitCollection = regex.Matches(sourceStr);
string digitStr = "";
foreach (Match digitMatch in digitCollection)
{
digitStr += digitMatch;
}
return digitStr;
}
}
}
这时form中的buttonTest_Click方法里对test_.collectData和 test_.fetchDigit的赋值需要改一下。代修后的代码如下。
private Test test_ = new Test();//为了调用和传递需要新建一个实例
private DataDeal dataDeal_ = new DataDeal();//为了调用和传递需要新建一个实例
private void buttonTest_Click(object sender, EventArgs e)
{
test_.collectData = dataDeal_.collectData;
test_.fetchDigit = dataDeal_.fetchDigit;
test_.test();
}
代码成功运行。
有时如果在DataDeal类的collectData和fetchDigit中作了一些其他操作,可能会引起异常,比如实例方法的委托不能具有空“this”
这时,就需要在form中的赋值过程作一些处理,这可以通过lambda表达式实现,修改后代码如下。
test_.collectData = () => { return dataDeal_.collectData(); };
test_.fetchDigit = (sourceStr) => { return dataDeal_.fetchDigit(sourceStr); };
所以对于类之间的函数传递,其实就是对于属性的操作,只是类型变成了Func和Action等委托类型。
其中要注意的是:
Func用于有返回值的。
Action用于没有返回值的。
转载请注明出处http://blog.csdn.net/xxdddail/article/details/9849111
C#利用lambda表达式将函数作为参数或属性跨类传递的更多相关文章
- [二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口
函数式接口详细定义 package java.lang; import java.lang.annotation.*; /** * An informative annotation type use ...
- 优雅实现INotifyPropertyChanged接口——利用Lambda表达式
原文:优雅实现INotifyPropertyChanged接口--利用Lambda表达式 参考文章 在14年的时候,曾经读过上面的参考文章,不过当时并没有怎么理解,慢慢地也就将这篇文章忘诸脑后了. 直 ...
- java8函数式接口详解、函数接口详解、lambda表达式匿名函数、方法引用使用含义、函数式接口实例、如何定义函数式接口
函数式接口详细定义 函数式接口只有一个抽象方法 由于default方法有一个实现,所以他们不是抽象的. 如果一个接口定义了一个抽象方法,而他恰好覆盖了Object的public方法,仍旧不算做接口的抽 ...
- 【C++】C++中的lambda表达式和函数对象
目录结构: contents structure [-] lambda表达式 lambda c++14新特性 lambda捕捉表达式 泛型lambda表达式 函数对象 函数适配器 绑定器(binder ...
- Lambda表达式和函数试接口的最佳实践 · LiangYongrui's Studio
1.概述 本文主要深入研究java 8中的函数式接口和Lambda表达式,并介绍最佳实践. 2.使用标准的函数式接口 包java.util.function中的函数是接口已经可以满足大部分的java开 ...
- 委托到Lambda的进化: ()=> {} 这个lambda表达式就是一个无参数的委托及具体方法的组合体。
1.原始的委托 (.net 1.0) using System; using System.Collections.Generic; using System.ComponentModel; usin ...
- Python:lambda表达式(匿名函数)
lambda表达式: 通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数. 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中 ...
- 还看不懂同事的代码?Lambda 表达式、函数接口了解一下
当前时间:2019年 11月 11日,距离 JDK 14 发布时间(2020年3月17日)还有多少天? // 距离JDK 14 发布还有多少天? LocalDate jdk14 = LocalDate ...
- lambda表达式 匿名函数
lambda函数是一种快速定义单行最小函数的方法,是从Lisp借鉴而来的,可以用在任何需要函数的地方. 基础 lambda语句中,冒号前是参数,可以有多个,用逗号分割:冒号右边是返回值. lambda ...
随机推荐
- linux aio
前几天nginx的0.8.x正式成为stable,然后看了下代码,发现0.8加入了linux native aio的支持,我们知道在linux下有两种aio,一种是glibc实现的aio,这个比较烂, ...
- [原]Unity3D深入浅出 - GUI控件
Unity的GUI类提供了丰富的界面控件,通过组合这些控件,完成和用户交互的界面. Lable:绘制文本和图片 Box:绘制一个图形框 Button:绘制一个响应单击事件的按钮 RepeatButto ...
- jquery 图片放大
上一篇是关于手风琴效果的,但是有时候我们需要放大的图片大小不规律,想要在屏幕中间显示大图. 图片放大可以做为单独的效果,也可以和其他的效果结合起来.比如Demo 里的Demo5.html是以图片无缝切 ...
- IE的体系和webrowser
IE的体系 WebBrowser Host首先,必须有COM的基础知识,因为IE本身就是COM技术的典型应用.我们看到最上层是WebBrowser的宿主(Host),也就是任何你想重用(ReUse)w ...
- Java [leetcode 35]Search Insert Position
题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- C#实现无物理边距真正可打印区域的绘图\打印程序开发
经常在开发实际的应用程序中,需要用到图形绘制和打印程序.如何实现完整的精确打印和绘图是需要注意许多细节地方的.最近在遇到打印问题的时候,仔细研究一阵,总结这篇博文,写得有点杂乱,看文要还请费点神. 基 ...
- Oracle ORA-00119和ORA-00132的解决方案
今天在启动服务器上的ORACLE时遇到如下错误: SQL> startup; ORA-00119: invalid specification for system parameter LOCA ...
- 工作流设计 zt
工作流设计 业务流程管理模块是本平台的重要组成部分,要实现将已经发布的标准中规范化的流程转化为具体计算机中的流程从而实现流程的自动运转,将标准化成果与员工的日常工作紧密结合起来,具有重要意义. 业务流 ...
- 如何理解Stay hungry,stay foolish?
People know about this words because of Steve Jobs.Me too. Hungry,对知识我们一般不会用hungry,我们会用curious,什么时候我 ...
- 关于Aggregate 的一点用法
比如 我们要 将数组或者对象中的某列或某属性 的值取出,然后 用 逗号隔开. 1.通常我们可能会用for 或foreach 来循环,然后将取出的值并添加至StringBuilder 2.用Aggreg ...