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. matplotlib 高阶之path tutorial

    目录 Bezier example 用path来画柱状图 随便玩玩 import matplotlib.pyplot as plt from matplotlib.path import Path i ...

  2. 前后端java+vue 实现rsa 加解密与摘要签名算法

    RSA 加密.解密.签名.验签.摘要,前后端java+vue联调测试通过 直接上代码 // 注意:加密密文与签名都是唯一的,不会变化.// 注意:vue 端密钥都要带pem格式.java 不要带pem ...

  3. 4.1.4 统计“锦途网”旅游线路平均价格,并采用尽可能多的方式将该价格赋给用户会话变量 @avg_short_price,并输出该变量

    查看本章节 查看作业目录 需求说明: 统计"锦途网"旅游线路平均价格,并采用尽可能多的方式将该价格赋给用户会话变量 @avg_short_price,并输出该变量 在 MySQL ...

  4. Android物联网应用程序开发(智慧园区)—— 登录界面开发

    效果: 布局代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  5. Java高级程序设计笔记 • 【第1章 IO流】

    全部章节   >>>> 本章目录 1.1 File类访问文件 1.1.1 File 类 1.1.2 File 类方法 1.1.3 实践练习 1.2 文件过滤器 1.2.1 Fi ...

  6. JDK中的BitMap实现之BitSet源码分析

    前提 本文主要内容是分析JDK中的BitMap实现之java.util.BitSet的源码实现,基于JDK11编写,其他版本的JDK不一定合适. 文中的图比特低位实际应该是在右边,但是为了提高阅读体验 ...

  7. 疯狂的类构造器Builder模式,链式调用

    疯狂的类构造器 最近栈长在做 Code Review 时,发现一段创建对象的方法: Task task = new Task(112, "紧急任务", "处理一下这个任务 ...

  8. Spring @Valid 和 @Validated 的区别和使用

    两者区别 @Valid @Validated 标准 标准JSR-303规范 增强JSR-303规范 包 javax.validation org.springframework.validation ...

  9. rsync配置文件讲解

    1.安装rysnc 一般在安装系统时rsync是安装上(yum安装) 2.     vim /etc/xinetd.d/rsync 在这个路径下有配置文件 service rsync { disabl ...

  10. linux 之 nginx安装步骤

    配置规划 用户 lzh 用户目录  /lzh 下载 进入官网下载nginx http://nginx.org/download/ 安装 解压 cd /lzh/app tar -zxvf nginx-1 ...