一、我们在使用C#的过程中,不可避免的用到了委托。

委托的本质是什么呢?

从语法上看,委托是对方法的抽象封装,例如:public void print1(),public void print2(),我们可以抽象化,public delegate void Print();用Print p来声明,委托需要实例化和调用,类似于c++中的函数模板;

从功能上看,委托主要是方法的指针,用以方便实现函数的回调、调用(异步等)。

直观上,委托的作用有方法(函数)回调、方法(函数)接收值(listener),方法(函数)多线程调用、传值等等。

本质上,delegate是一个类,同关键字class和interface平级的,这个类中包含方法所属的类实例的地址、方法的地址,以及下个委托的引用(因此委托是链式结构的)。

以下我们以实例展开委托声明、实例化、调用等技巧。

二、委托的声明

C#中借用delegate关键字,C#的编译器一遇到delegate,在翻译C#的时候就被翻译成类似于函数模板的东西。delegate刻意定义在类的内部,也可以定义在外部,和类没有关系。

public delegate void Print();              //无参数,无返回值的委托

public delegate <out string>Print();   //无参数,返回string的委托

public delegate<out string,in string>Print(string str);  //string参数,返回string的委托

.net3.,系统定义了无参和有参的委托Action和Func关键字

public Action  本质就是public delegate void的转定义

public Func    本质是public delegate <out string>

用法:

public Action<string> action;

public Func<string> func;

委托的方法必须在形式上和委托的声明一致

三、委托的赋值

public NoparamDelegate noParam = new NoparamDelegate(doSomething_forNoparamDelegate);    //new 实现

public NoparamDelegate noparam_for_lambd = () =>                                                                            //lambd表达式直接赋值
{
Console.WriteLine("no param,form lambd,test");
};
public NoparamDelegate noparam_for_function = doSomething_function; //方法直接赋值 public static void doSomething_forNoparamDelegate()
{
Console.WriteLine("no param,form test");
} public static void doSomething_function()
{
Console.WriteLine("no param,form function, test");
}

四、委托的调用

//委托调用-普通方法
doSomething();

//委托调用invoke
doSomething?.Invoke();
oneParam?.Invoke("one param,form test");

//异步调用

doSomething?.BeginInvoke(complete, null); //object标识附加信息
IAsyncResult ret = func_oneparam?.BeginInvoke("transmit赋值,异步调用,", null, null); //ret.IsCompleted, ret.AsyncWaitHandle.WaitOne(100)等用于轮询过程
Console.WriteLine("开始其他工作!..");
string strRet = func_oneparam?.EndInvoke(ret);
Console.WriteLine(strRet);

委托的BeginInvoke有两个或三个参数(重载)

param1:string,参数

param2:回调函数,异步方法完成后调用

param3:object形式的附加信息

返回值:IAsyncResult 类型,我们记作ret

ret中有属性IsCompleted和AsyncWaitHandle,可以用以轮询异步方法调用的方法

string strRet = func_oneparam?.EndInvoke(ret);  //在需要的地方获取异步执行结果,如果没执行完,将阻塞调用线程,直至获取结果

五、委托链

对于多个委托方法,.net定义了委托链的概念

Delegate主要定义了Combine(简写+=),Remove(简写-=)等方法

Action delegateSet = null;
delegateSet = (Action)Delegate.Combine(actionChainOne,actionChainTwo);

下面是一段示例代码:

  public class Test
{
public delegate void Print();
Print p;
public void method1()
{
Console.WriteLine("m 1");
}
public void method2()
{
Console.WriteLine("m 2");
} public void method3()
{
Console.WriteLine("m 3");
}
public void method4()
{
Console.WriteLine("m 4");
} public void combine()
{
p += method1;
p += method2;
p += method3;
p += method4;
}
public void run()
{
Delegate[] myDelegates = p.GetInvocationList();
foreach (var myDelegate in myDelegates)
{
Print m1 = (Print)myDelegate;//注意此处需强转
m1.Invoke();
}
}
}
class Program
{
static void Main(string[] args)
{
Test test = new Test();
test.combine();
test.run(); Console.ReadKey();
}
}

有交流沟通,请加群:568055323

C#深度学习の委托深度解析的更多相关文章

  1. 深度学习论文翻译解析(四):Faster R-CNN: Down the rabbit hole of modern object detection

    论文标题:Faster R-CNN: Down the rabbit hole of modern object detection 论文作者:Zhi Tian , Weilin Huang, Ton ...

  2. 深度学习论文翻译解析(五):Siamese Neural Networks for One-shot Image Recognition

    论文标题:Siamese Neural Networks for One-shot Image Recognition 论文作者: Gregory Koch   Richard Zemel Rusla ...

  3. 深度学习论文翻译解析(八):Rich feature hierarchies for accurate object detection and semantic segmentation

    论文标题:Rich feature hierarchies for accurate object detection and semantic segmentation 标题翻译:丰富的特征层次结构 ...

  4. 深度学习论文翻译解析(九):Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition

    论文标题:Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 标题翻译:用于视觉识别的深度卷积神 ...

  5. 深度学习论文翻译解析(十三):Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks

    论文标题:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 标题翻译:基于区域提议(Regi ...

  6. 深度学习论文翻译解析(十八):MobileNetV2: Inverted Residuals and Linear Bottlenecks

    论文标题:MobileNetV2: Inverted Residuals and Linear Bottlenecks 论文作者:Mark Sandler Andrew Howard Menglong ...

  7. 学习笔记︱Nvidia DIGITS网页版深度学习框架——深度学习版SPSS

    DIGITS: Deep Learning GPU Training System1,是由英伟达(NVIDIA)公司开发的第一个交互式深度学习GPU训练系统.目的在于整合现有的Deep Learnin ...

  8. 一天搞懂深度学习-训练深度神经网络(DNN)的要点

    前言 这是<一天搞懂深度学习>的第二部分 一.选择合适的损失函数 典型的损失函数有平方误差损失函数和交叉熵损失函数. 交叉熵损失函数: 选择不同的损失函数会有不同的训练效果 二.mini- ...

  9. 【神经网络与深度学习】深度学习实战——caffe windows 下训练自己的网络模型

    1.相关准备 1.1 手写数字数据集 这篇博客上有.jpg格式的图片下载,附带标签信息,有需要的自行下载,博客附带百度云盘下载地址(手写数字.jpg 格式):http://blog.csdn.net/ ...

随机推荐

  1. Verification and validation

    Verification Verification is the process to make sure the product satisfies the conditions imposed a ...

  2. mysql length和char_length

    length和char_length都是为了统计字符串的长度,length是按照字节来统计,char_lenght是按照字符来统计. 位(bit):计算机储存的最小单位. 字节(byte):计算机处理 ...

  3. 工作笔记-table问题汇总(vue单文件组件)

    1.vue: computed里定义的数据,在其他地方不能再重新赋值,会报错: Computed property "xxxxxx" was assigned to but it ...

  4. RBAC 几种常见的控制权限模型

    1. 几种常见的权限模型 2. ACL 和 RBAC 对比 3. RBAC 权限模型的优势 (1)简化了用户和权限的关系 (2).易于扩展 易于维护 4.优势(给权限和收回权限) 5.架构

  5. MySQL数据库锁类型

    锁概念 : 当高并发访问同一个资源时,可能会导致数据不一致,需要一种机制将用户访问数据的顺序进行规范化,以保证数据库数据的一致性.锁就是其中的一种机制. 一个栗子 :以买火车票为例,火车票可面向广大消 ...

  6. 如何用ABP框架快速完成项目(8) - 用ABP一个人快速完成项目(4) - 能自动化就不要手动 - 使用自动化测试(BDD/TDD)

    做为一个程序员, 深深知道计算机自动化的速度是比人手动的速度快的, 所以”快速”完成项目的一个重要武器就是: 能自动化就不要手动.   BDD/TDD有很多优势, 其中之一就是自动化, 我们这节文章先 ...

  7. Mysql LIMIT 分页

    格式: LIMIT index, size     // index:从哪一行(第几条)开始查,size:多少条 分页: LIMIT (currentPage-1)*pageSize, pageSiz ...

  8. Redis 开启远程连接

    默认 bind 127.0.0.1 即绑定本机 IP,只能本机访问,你也可以绑定别的 IP 地址,如果注释掉,表示不限制 IP,所有 IP 都能访问 # ~~~ WARNING ~~~ If the ...

  9. Android样式主题及自定义属性

    一.Selector——图形.颜色选择器 语法 <selector>   <item android:drawable=“drawableResA” android:state_xx ...

  10. ionic 确认提示操作框

    //确认框 .factory('ActionSheet', function ($ionicActionSheet, TipsPort, Service,Loading) { var ActionSh ...