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 ...
随机推荐
- Jmeter性能测试场景的创建和运行
目录 性能测试场景的分析 项目背景 Jmeter指标 性能测试场景的设计以及准备 性能测试的总结 性能测试场景的分析 项目背景 实际工作中,我们拿到一个项目一般来说都会是项目经理说XXX来进行一下 ...
- uniapp蓝牙传输中文乱码问题
问题描述:app接收到蓝牙传出过来的二进制数据,1.app进行arrbuff转成16进制字符串 // ArrayBuffer转16进度字符串示例 function ab2hex(buffer) { c ...
- uniapp解决测评有组件导出风险,解决APP反编译,回编译后app无法打开的问题
1.APP反编译 使用hbx云打包,打包出apk 拿到apk后,先下载反编译工具 https://pan.baidu.com/s/1A5D8x_pdSELlHYl-Wl6Xnw 提取码 6vzd 使用 ...
- Linux的基本目录结构
- centos6.5搭建Apache-虚拟主机
一.配置基于域名的虚拟用户 1.创建虚拟用户的网页根目录 cd /usr/local/httpd/htdocs/ mkdir benetcom cd benetcom echo "<h ...
- TypeScript 中文教程之缩小----部分翻译自TS官方
Narrowing概念:字面意思是缩小,可以理解为细化或者您觉得更好的代名词. TS官方在这里做了很详细的说明,文字较多,简单以图片概括: typeof type guards 类型防护过程,可以通 ...
- ORACLE 之 按月循环执行操作
DECLARE i number; BEGIN i:= 201705; WHILE i <202104 LOOP if i=201713 then i:=201801; elsif i=2018 ...
- js字符串首字母大写的不同写法
写法一: let name = 'hello' name.charAt(0).toUpperCase() + name.slice(1) 写法二: let name = 'hello' name.sl ...
- 【Spring专场】「MVC容器」不看源码就带你认识核心流程以及运作原理
前提回顾 之前已经写了很多问斩针对于SpringMVC的的执行原理和核心流程,在此再进行冗余介绍就没有任何意义了,所以我们主要考虑的就是针对于SpringMVC还没但大框架有介绍的相关内容解析分析和说 ...
- Sharding Sphere的分库分表
什么是 ShardingSphere? 1.一套开源的分布式数据库中间件解决方案 2.有三个产品:Sharding-JDBC 和 Sharding-Proxy 3.定位为关系型数据库中间件,合理在分布 ...