Func是一个委托,委托里面可以存方法,Func<string,string>或Func<string,string,int,string>等

前几个是输入参数,最后一个是返回参数

以Func<int,bool>为例:

private bool IsNumberLessThen5(int number)

{return number < 5;}

Func<int,bool> f1 = IsNUmberLessThen5;

调用:
bool flag= f1(4);

//以下是其它的用法,与IsNumberLessThen5作用相同。只是写法不同而已。
Func<int, bool> f2 = i => i < 5;
Func<int, bool> f3 = (i) => { return i < 5; };
Func<int, bool> f4 = delegate(int i) { return i < 5; };

多参数的使用方法

Func<int,int ,int> Coun = (x,y) =>
{
return x + y;
};

自己项目中实例

类库的一个方法需要用到Server.MapPath返回的路径,但是类库中无法直接使用Server.MapPath方法

于是在web中定义一个方法,接收一个相对路径字符串,返回相对于根目录的字符串

Func<string, string> getphysicsPath = p =>
{
  return Server.MapPath(p);
};

  

在类库的中:

    public class SaveUploadFile
{
public static string SaveImage(HttpPostedFileBase file, Func<string, string> getPath)
{ DateTime now = DateTime.Now;
string newFileName = string.Empty;
string fileType = Path.GetExtension(file.FileName);
string suijishu = Math.Abs(Guid.NewGuid().GetHashCode()).ToString();
newFileName = now.ToString("yyyyMMddHHmmss") + "_" + suijishu + fileType; string path = CommConfig.UploadImageUrl + "/" + now.ToString("yyyyMMdd"); string physicsPath = getPath(path);//调用传入的方法计算路径 string fullPath = physicsPath + "/" + newFileName;
string newFilePath = path + "/" + newFileName; if (!Directory.Exists(physicsPath))
{
Directory.CreateDirectory(physicsPath);
} file.SaveAs(fullPath); return newFilePath;
}
}

  

  

最后在web中

var file = Request.Files[0];

string newFilePath= SaveUploadFile.SaveImage(file, getphysicsPath);

  

Func<>用法的更多相关文章

  1. MVC ---- 理解学习Func用法

    //Func用法 public static class FuncDemo{ public static void TestFunc(){ //数据源 List<User> usList ...

  2. Action与Func 用法

    //vs2017 + framework4.6.2 //zip    https://github.com/chxl800/ActionFuncDemo //源文件git   https://gith ...

  3. action func用法记记

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void s ...

  4. 委托小结及Func用法

    首先,委托是一种类型,由关键字delegate声明.确切的说,委托是一种可用于封装命名或者匿名方法的引用类型.  它类似于 C++ 中的函数指针,而且是类型安全和可靠的.       委托类型的声明与 ...

  5. c# 委托(Func、Action)

    以前自己写委托都用 delegate, 最近看组里的大佬们都用 Func , 以及 Action 来实现, 代码简洁了不少, 但是看得我晕晕乎乎. 花点时间研究一下,记录一下,以便后期的查阅. 1.F ...

  6. python note 11 函数名的使用、闭包、迭代器

    1.函数名就是一个变量 def func(): print("我是一个小小的函数") a = func print(a) #输出变量存放地址 <function func a ...

  7. Python内建函数一

    内建函数 1. abs(number) 用法:返回数字的绝对值 2. all(iterable) 用法:如果iterable的所有元素都是真值,就返回True,否则返回False 3. any(ite ...

  8. golang中type关键字使用

    type关键字使用 type是go语法里的重要而且常用的关键字,type绝不只是对应于C/C++中的typedef.搞清楚type的使用,就容易理解go语言中的核心概念struct.interface ...

  9. C#中Predicate<T>与Func<T, bool>泛型委托的用法实例

    本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...

随机推荐

  1. Denoising Diffusion Probabilistic Models (DDPM)

    目录 概 主要内容 Diffusion models reverse process forward process 变分界 损失求解 最后的算法 细节 代码 Ho J., Jain A. and A ...

  2. Java初学者作业——定义一个计算器类, 实现计算器类中加、 减、 乘、 除的运算方法, 每个方法能够接收2个参数。

    返回本章节 返回作业目录 需求说明: 定义一个计算器类, 实现计算器类中加. 减. 乘. 除的运算方法, 每个方法能够接收2个参数. 实现思路: 定义计算器类. 定义计算器类中加.减.乘.除的方法. ...

  3. Spring Boot 整合 Fisco Bcos(区块链)

    简介 FISCO BCOS是由国内企业主导研发.对外开源.安全可控的企业级金融联盟链底层平台,由金链盟开源工作组协作打造,并于2017年正式对外开源. 目前,成熟的区块链的平台不少,之所以选择FISC ...

  4. 线性基(Linear Basis)学习笔记

    前言 我看网络上没有什么非常系统的教学,可能是我太菜了吧,现在才学,做个记录给自己看. 简略介绍 一个数集能两两异或,能表出许多新的数. 线性基是一个集合,能够在记录最少的数的基础上,表示出一个等价的 ...

  5. python + pytest + allure生成测试报告

    pytest结合allure生成测试报告 环境搭建 要安装java环境,版本要是jdk1.8的,配置好java环境变量,不然输入allure命令会报错,JAVA_HOME环境,自行配置 安装allur ...

  6. java POJO中 Integer 和 int 的不同,用int还是用Integer

    https://www.jianshu.com/p/ff535284916f [int和Integer的区别] int是java提供的8种原始类型之一,java为每个原始类型提供了封装类,Intege ...

  7. Centos7 安装 brctl 工具

    [root@docker-node1 ~]# brctl show -bash: brctl: command not found [root@docker-node1 ~]# yum -y inst ...

  8. windows更改pip源(可用)

    中国科学技术大学 : https://pypi.mirrors.ustc.edu.cn/simple 清华:https://pypi.tuna.tsinghua.edu.cn/simple 豆瓣:ht ...

  9. spring cloud bus 消息总线 动态刷新配置文件 【actuator 与 RabbitMQ配合完成】

    1.前言 单机刷新配置文件,使用actuator就足够了 ,但是 分布式微服务 不可能是单机 ,将会有很多很多的工程 ,无法手动一个一个的发送刷新请求, 因此引入了消息中间件 ,常用的 消息中间件 是 ...

  10. ubuntu 18.04 关掉sudo密码

    关掉sudo密码 $ sudo visudo 找到如下行 %sudo ALL=(ALL:ALL) ALL 修改为 %sudo ALL=(ALL:ALL) NOPASSWD:ALL 这样sudo用户组的 ...