在编码时,由于开始是在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表达式将函数作为参数或属性跨类传递的更多相关文章

  1. [二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口

    函数式接口详细定义 package java.lang; import java.lang.annotation.*; /** * An informative annotation type use ...

  2. 优雅实现INotifyPropertyChanged接口——利用Lambda表达式

    原文:优雅实现INotifyPropertyChanged接口--利用Lambda表达式 参考文章 在14年的时候,曾经读过上面的参考文章,不过当时并没有怎么理解,慢慢地也就将这篇文章忘诸脑后了. 直 ...

  3. java8函数式接口详解、函数接口详解、lambda表达式匿名函数、方法引用使用含义、函数式接口实例、如何定义函数式接口

    函数式接口详细定义 函数式接口只有一个抽象方法 由于default方法有一个实现,所以他们不是抽象的. 如果一个接口定义了一个抽象方法,而他恰好覆盖了Object的public方法,仍旧不算做接口的抽 ...

  4. 【C++】C++中的lambda表达式和函数对象

    目录结构: contents structure [-] lambda表达式 lambda c++14新特性 lambda捕捉表达式 泛型lambda表达式 函数对象 函数适配器 绑定器(binder ...

  5. Lambda表达式和函数试接口的最佳实践 · LiangYongrui's Studio

    1.概述 本文主要深入研究java 8中的函数式接口和Lambda表达式,并介绍最佳实践. 2.使用标准的函数式接口 包java.util.function中的函数是接口已经可以满足大部分的java开 ...

  6. 委托到Lambda的进化: ()=> {} 这个lambda表达式就是一个无参数的委托及具体方法的组合体。

    1.原始的委托 (.net 1.0) using System; using System.Collections.Generic; using System.ComponentModel; usin ...

  7. Python:lambda表达式(匿名函数)

    lambda表达式: 通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数. 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中 ...

  8. 还看不懂同事的代码?Lambda 表达式、函数接口了解一下

    当前时间:2019年 11月 11日,距离 JDK 14 发布时间(2020年3月17日)还有多少天? // 距离JDK 14 发布还有多少天? LocalDate jdk14 = LocalDate ...

  9. lambda表达式 匿名函数

    lambda函数是一种快速定义单行最小函数的方法,是从Lisp借鉴而来的,可以用在任何需要函数的地方. 基础 lambda语句中,冒号前是参数,可以有多个,用逗号分割:冒号右边是返回值. lambda ...

随机推荐

  1. linux下进程相关操作

    一.定义和理解 狭义定义:进程是正在运行的程序的实例. 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动. 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的 ...

  2. poj3274

    很不错的hash 优化有两个方面:1.根据题目换一个更优化的算法 2.在算法运行过程中优化 这题除了暴力好像没别的办法了吧? 但是暴力也是有策略的! 到第i只牛特征为j的总数为sum[i,j]; 找到 ...

  3. linq .dbml转化成sql脚本

    public String ConvertDBMLToSqlScript(System.Data.Linq.DataContext DBContext)  {         String DBCon ...

  4. JavaScript中定时器

    JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成.它们向任务队列添加定时任务. setTimeout() ...

  5. Android 多种方式正确的加载图像,有效避免oom

    图像加载的方式: Android开发中消耗内存较多一般都是在图像上面,本文就主要介绍怎样正确的展现图像减少对内存的开销,有效的避免oom现象.首先我们知道我的获取图像的来源一般有三种源头:1.从网络加 ...

  6. 自动化测试(二):QTP验证点

    1 程序自带验证点 自带验证点:图形界面insert  ->  checkpoint Standard Checkpoint 标准验证:用于检查测试对象的属性 Text Checkpoint 文 ...

  7. iOS7适配之设计篇

    (注:文章简要翻译自 Apple <iOS 7 UI Transition Guide>,由于该文档为开发者预览版,并非最终文档,所以 iOS7 正式上线可能有部分不同) 准备工作 iOS ...

  8. NOIP2011 观光公交

    3.观光公交 (bus.cpp/c/pas) 风景迷人的小城 Y 市,拥有 n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特 意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 ...

  9. 了解常见的 Azure 灾难

    以下内容涵盖多种不同类型的灾难情况.数据中心故障不是应用程序范围内发生故障的唯一原因.设计不良或管理错误也会导致中断.请在恢复计划的设计和测试阶段设想可能导致故障的原因,这样做很重要.一个好的计划可充 ...

  10. Tomcat普通用户启动注意事项

    今天项目部署上线,老大跟我建议说不要使用root用户部署,试用普通用户运行.刚开始没想什么,后来部署的时候碰到各种权限问题. 记录一下,以防忘记了. 1.使用普通用户启动失败. 首先不用想就去$TOM ...