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 ...
随机推荐
- 1106. Two Teams(dfs 染色)
1106 结点染色 当前结点染为黑 朋友染为白 依次染下去 这题是为二分图打基础吧 #include <iostream> #include<cstdio> #include ...
- 算法的时间复杂度(大O表示法)
定义:如果一个问题的规模是n,解这一问题的某一算法所需要的时间为T(n),它是n的某一函数 T(n)称为这一算法的“时间复杂性”. 当输入量n逐渐加大时,时间复杂性的极限情形称为算法的“渐近时间复杂性 ...
- Month Calendar
http://www.codeproject.com/Articles/10840/Another-Month-Calendar#xx4614180xx Another Month Calendar ...
- 10、Android数据存储
课程目标: 掌握Android中数据存储的几种方式 熟练使用PreferenceActivity&PreferenceScreen做专业的Setting功能 熟练使用SQLite3来存储数据 ...
- [Irving]SqlServer 标量函数 详解【转】
--创建用户定义函数.这是一个已保存 Transact-SQL 或公共语言运行时 (CLR) 例程,--该例程可返回一个值.用户定义函数不能用于执行修改数据库状态的操作.--与系统函数一样,用户定义函 ...
- Channel 详解
java.nio.channels.FileChannel封装了一个文件通道和一个FileChannel对象,这个FileChannel对象提供了读写文件的连接. 1.接口 2.通道操作 a.所有通道 ...
- CentOS上安装MyCat-MySQL
1.安装JDK,要求JDK7以上. 2.下载MyCat,地址. 3.解压Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz,到usr/local/ ...
- php 实现文件下载,兼容IE、Firefox、Chrome等浏览器
一.下载任意文件: Header ( "Content-type: application/octet-stream" ); $ua = $_SERVER ["HTTP_ ...
- cocos2d-x3.0+Eclipse配置说明
假如我们已经装了JavaJDK.Cygwin,也解压了2013-08-27之后最新的AndroidSDK,其实最新的AndroidSDK已经集成了eclipse,eclipse里面已经配置好了Andr ...
- HDU-2262 Where is the canteen 概率DP,高斯消元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2262 题意:LL在一个迷宫里面转,每次走向周围能走的点的概率都是一样的,现在LL要随机的走到cante ...