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. Cookie、Session、Token、JWT

    什么是认证(Authentication)------->就是验证当前用户的身份,证明"你是你自己" 互联网中的认证: 用户名密码登录 邮箱发送登录链接 手机号接收验证码 只 ...

  2. Deep Linear Networks with Arbitrary Loss: All Local Minima Are Global

    目录 问题 假设和重要结果 证明 注 Laurent T, Von Brecht J H. Deep linear networks with arbitrary loss: All local mi ...

  3. [决策树]西瓜数据graphviz可视化实现

    [决策树]西瓜数据graphviz可视化实现 一.问题描述: 使用西瓜数据集构建决策树,并将构建的决策树进行可视化操作. 二.问题简析: 首先我们简单的介绍一下什么是决策树.决策树是广泛用于分类和回归 ...

  4. jQuery 中使用 DOM 操作节点,对页面中的表格实现增、删、查、改操作

    查看本章节 查看作业目录 需求说明: 在 jQuery 中使用 DOM 操作节点,对页面中的表格实现增.删.查.改操作 点击"增加"超链接时,将表格中的第一条数据添加到表格的末尾 ...

  5. Windows Server 2016 服务器安装IIS

    1. 打开服务器管理器,点击[添加角色和功能选项]  2.进入[添加角色和功能向导]页面,点击"下一步"  3.安装类型选择[基于角色或基于功能的安装],点击"下一步&q ...

  6. ProtoBuf3语法指南(Protocol Buffers)_上

    0.说明 ProtoBuf3语法指南, 又称为proto3, 是谷歌的Protocol Buffers第3个版本. 本文基于官方英文版本翻译, 加上了自己的理解少量修改, 一共分为上下两部分. 1.序 ...

  7. 初识python: 列表(list)

    使用列表函数写一个"购物车"小程序: #!/user/bin env python # author:Simple-Sir # 20180908 ''' 需求: 1.启动程序后,让 ...

  8. Powershell 【控制台常用方法】

    1 function Pause(){ 2 [System.Console]::Write('按任意键继续...') 3 [void][System.Console]::ReadKey(1) 4 } ...

  9. Vulnhub系列:chili

    0x01 靶机信息 靶机:chili难度:简单下载:https://www.vulnhub.com/entry/chili-1,558/ 靶机描述: 0x02 信息收集 nmap扫描存活主机确定靶场i ...

  10. 机器学习&恶意代码动态检测

    目录 写在前面 1 基于API调用的统计特征 2 API序列特征 3 API调用图 4 基于行为的特征 references: 写在前面 对恶意程序动态检测方法做了概述, 关于方法1和2可以参考阿里云 ...