Func<>用法
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<>用法的更多相关文章
- MVC ---- 理解学习Func用法
//Func用法 public static class FuncDemo{ public static void TestFunc(){ //数据源 List<User> usList ...
- Action与Func 用法
//vs2017 + framework4.6.2 //zip https://github.com/chxl800/ActionFuncDemo //源文件git https://gith ...
- action func用法记记
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void s ...
- 委托小结及Func用法
首先,委托是一种类型,由关键字delegate声明.确切的说,委托是一种可用于封装命名或者匿名方法的引用类型. 它类似于 C++ 中的函数指针,而且是类型安全和可靠的. 委托类型的声明与 ...
- c# 委托(Func、Action)
以前自己写委托都用 delegate, 最近看组里的大佬们都用 Func , 以及 Action 来实现, 代码简洁了不少, 但是看得我晕晕乎乎. 花点时间研究一下,记录一下,以便后期的查阅. 1.F ...
- python note 11 函数名的使用、闭包、迭代器
1.函数名就是一个变量 def func(): print("我是一个小小的函数") a = func print(a) #输出变量存放地址 <function func a ...
- Python内建函数一
内建函数 1. abs(number) 用法:返回数字的绝对值 2. all(iterable) 用法:如果iterable的所有元素都是真值,就返回True,否则返回False 3. any(ite ...
- golang中type关键字使用
type关键字使用 type是go语法里的重要而且常用的关键字,type绝不只是对应于C/C++中的typedef.搞清楚type的使用,就容易理解go语言中的核心概念struct.interface ...
- C#中Predicate<T>与Func<T, bool>泛型委托的用法实例
本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...
随机推荐
- 第四十三个知识点:为AES描述一些基础的(可能无效)的对抗侧信道攻击的防御
第四十三个知识点:为AES描述一些基础的(可能无效)的对抗侧信道攻击的防御 原文地址:http://bristolcrypto.blogspot.com/2015/07/52-things-numbe ...
- Log4j使用的方法
一.什么是log4j Log4J是Apache的一个开放源代码的项目.通过使用Log4J,程序员可以控制日志信息输送的目的地,包括控制台,文件,GUI组件和NT事件记录器,也可以控制每一条日志的输出格 ...
- Missing Data in Kernel PCA
目录 引 主要内容 关于缺失数据的导数 附录 极大似然估计 代码 Sanguinetti G, Lawrence N D. Missing data in kernel PCA[J]. europea ...
- 编写Java程序,读取文本文档的内容,去除文本中包含的“广告”字样,把更改后的内容保存到一个新的文本文档中
查看本章节 查看作业目录 需求说明: 读取文本文档的内容,去除文本中包含的"广告"字样,把更改后的内容保存到一个新的文本文档中 实现思路: 在main() 方法中,使用 new F ...
- MySQL数据库安装Version5.7
MySQL数据库版本: mysql-5.7.22-linux-glibc2.12-x86_64 Linux服务器系统: CentOS 7.4 64bit MySQL安装用户: mysql/aliyun ...
- ASP.NET+MVC入门踩坑笔记 (一) 创建项目 项目配置运行 以及简单的Api搭建
哈喽各位 我又回来了! 前段时间研究了下ASP.NET,刚开始也是随便找网上的各种教程来看,但是鉴于本人技术有限,还是走了相当长的一段弯路的.所以我写下了这篇文章.希望各位刚刚入坑的ASP.NET开发 ...
- Pytest_配置文件-pytest.ini(4)
pytest配置文件可以改变pytest的默认运行方式,它是一个固定的文件名称pytest.ini. 存放路径为项目的根目录 解决中文报错 在讲解配置文件的可用参数前,我们先解决一个高概率会遇到的问题 ...
- Linux中ssh登陆慢的两种原因
useDNS配置导致登陆慢 如果ssh server的配置文件(通常是 /etc/ssh/sshd_config )中设置 useDNS yes ,可能会导致 ssh 登陆卡住几十秒.将该配置项设为 ...
- 初识python 之 爬虫:使用正则表达式爬取“古诗文”网页数据
通过requests.re(正则表达式) 爬取"古诗文"网页数据. 详细代码如下: #!/user/bin env python # author:Simple-Sir # tim ...
- linux7,一台物理机上使用VM装多个虚拟机,始终只有一个虚拟机网络正常,其他虚拟机报错Error: Connection activation failed: No suitable device found for this connection.
今天在VM新装了一个虚拟机,结果发现原来的虚拟机连不上了,重启网络服务后报错 Error: Connection activation failed: No suitable device found ...