C#入门(3)
C#入门(3)
Delegates, Events, Lambda Expressions
最早的windows是使用c风格的函数指针来进行callback的,但是这样仅仅传递了一个内存中的地址,无法包含更多的信息,如类型安全等,而且容易引发crash.
所以.Net平台下的delegate可以指向静态方法或者是对象方法,一般包含三种重要信息:
- 需要调用的方法的地址
- 方法的参数
- 方法的返回值
代理可以支持同步方法和异步方法:
对于一个delegate,首先编译器会生成一个seal的类.具体如下:
public delegate string MyDelegate(bool a, bool b, bool c);
sealed class MyDelegate : System.MulticastDelegate
{
public string Invoke(bool a, bool b, bool c);
public IAsyncResult BeginInvoke(bool a, bool b, bool c,AsyncCallback cb, object state);
public string EndInvoke(IAsyncResult result);
}
// This is only pseudo-code!
public sealed class DelegateName : System.MulticastDelegate
{
public delegateReturnValue Invoke(allDelegateInputRefAndOutParams);
public IAsyncResult BeginInvoke(allDelegateInputRefAndOutParams,AsyncCallback cb, object state);
public delegateReturnValue EndInvoke(allDelegateRefAndOutParams,IAsyncResult result);
}
在生成的类中的三个方法中,Invoke()是负责同步(synchronized)的,该方法在使用正确的语法时,不需要显式调用.而剩下的两个BeginInvoke()和EndInvoke()是负责异步的(asynchronized),注意这里的BeginInvoke()后面会自动增加两个参数,一个是AsyncCallback,一个是object;而EndInvoke()的返回值则是delegate的返回值,参数则是BeginInvoke()的返回值.但是当使用out或者ref修饰的参数时,这样的参数也会传递给EndInvoke().
所以使用delegate声明,其实是继承了System.MulticastDelegate类和System.Delegate类.但是在程序中不能够显式继承自这两个类.下面给出了这两个类的部分:
public abstract class MulticastDelegate : Delegate
{
// Returns the list of methods "pointed to."
public sealed override Delegate[] GetInvocationList();
// Overloaded operators.
public static bool operator ==(MulticastDelegate d1, MulticastDelegate d2);
public static bool operator !=(MulticastDelegate d1, MulticastDelegate d2);
// Used internally to manage the list of methods maintained by the delegate.
private IntPtr _invocationCount;
private object _invocationList;
}
public abstract class Delegate : ICloneable, ISerializable
{
// Methods to interact with the list of functions.
public static Delegate Combine(params Delegate[] delegates);
public static Delegate Combine(Delegate a, Delegate b);
public static Delegate Remove(Delegate source, Delegate value);
public static Delegate RemoveAll(Delegate source, Delegate value);
// Overloaded operators.
public static bool operator ==(Delegate d1, Delegate d2);
public static bool operator !=(Delegate d1, Delegate d2);
// Properties that expose the delegate target.
public MethodInfo Method { get; }
public object Target { get; }
}
下表给出具体属性的含义:
Member | Meaning |
---|---|
Method | This property returns a System.Reflection.MethodInfo object that represents details of a static method maintained by the delegate. |
Target | If the method to be called is defined at the object level (rather than a static method), Target returns an object that represents the method maintained by the delegate. If the value returned from Target equals null, the method to be called is a static member. |
Combine() | This static method adds a method to the list maintained by the delegate. In C#, you trigger this method using the overloaded += operator as a shorthand notation. |
GetInvocationList() | This method returns an array of System.Delegate objects, each representing a particular method that may be invoked. |
Remove() RemoveAll() | These static methods remove a method (or all methods) from the delegate’s invocation list. In C#, the Remove() method can be called indirectly using the overloaded -= operator. |
Combine()是当给delegate增加回调函数时用的,调用+=会隐式调用Combine(),
使用delegates:
一般使用delegates的步骤为:
- 定义一种新的delegates类型,来支持回调
- 在使用delegates的类中定义一个该delegates的成员变量,可以是private也可以是public
- 定义一个辅助函数来使得调用者可以方便的调用回调
- 实现函数,在其中正确地调用delegates变量
Tips:
在c#中的界面开发中,由于父级控件的属性设置对于子级控件是有影响的,因此对于多级嵌套的层级控件,必须定义好add的顺序,先将后继控件加载然后加入到前级控件和先加载自身,然后加载后继控件是不同的。A-1-B-2-C和A-2-B-1-C,如这个表达式,ABC依次为嵌套关系。数字代表加载顺序。
这里的关键因素是一个属性,一般是tabControl或者是tabPage有的,叫AutoScaleMode,默认的是根据Font来进行改变,因此当父级元素和子级元素中的Font设置不一样,甚至是设置的一样,但是加入时仍然会按照字体的大小进行缩放处理.所以将该属性设为none可以避免不必要的麻烦.
这个Tips如果算是Bug,应该解了.
C#入门(3)的更多相关文章
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
- ABP入门系列(1)——学习Abp框架之实操演练
作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...
- Oracle分析函数入门
一.Oracle分析函数入门 分析函数是什么?分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计 ...
- Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数
上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...
- Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数
上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...
- Angular2入门系列教程4-服务
上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...
- wepack+sass+vue 入门教程(三)
十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...
- wepack+sass+vue 入门教程(二)
六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...
- wepack+sass+vue 入门教程(一)
一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
随机推荐
- 【网络爬虫】【java】微博爬虫(五):防止爬虫被墙的几个技巧(总结篇)
爬虫的目的就是大规模地.长时间地获取数据,跟我们正常浏览器获取数据相比,虽然机理相差不大,但总是一个IP去爬网站,大规模集中对服务器访问,时间一长就有可能被拒绝.关于爬虫长时间爬取数据,可能会要求验证 ...
- 浅析Apache/Tomcat/JBOSS/Nginx之区别
浅析Apache/Tomcat/JBOSS/Nginx服务器之区别 一.Apache和Tomcat的区别 Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上, ...
- Linux下共享库嵌套依赖问题 (转载)
转自:http://my.oschina.net/moooofly/blog/506466 问题场景: 动态库 librabbitmq_r.so 内部依赖动态库 libevent_core.so 和 ...
- NGUI研究院之UISprite和UITexture浅谈
NGUI的三大组件,UILabel.UISprite.UITexture,它们三个同时都继承UIWidget.先回到一个很郁闷的话题上,到底是优化DrawCall还是优化内存. UISprite : ...
- 自然语言处理(五)——实现机器翻译Seq2Seq完整经过
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 我只能说这本书太烂了,看完这本书中关于自然语言处理的内容,代码全部敲了一遍,感觉学的很绝望,代码也运行不了. 具体 ...
- Codeforces 1132D(二分模拟)
要点 二分显然,关键在于怎么判断 题解方法:开k个队列代表每个时间有哪些电脑会挂掉,这部分O(n)预处理一下,之后扫一遍时间,每个时间点贪心选取最靠前的会挂的电脑未雨绸缪给它充电,然后看看充电以后要不 ...
- Azkaban是什么?(一)
不多说,直接上干货! http://www.cnblogs.com/zlslch/category/938837.html Azkaban是什么? Azkaban是一套简单的任务调度服务,整体包括三 ...
- 需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________。A.客观地验证需求管理活动
需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是__________.A.客观地验证需求管理活动 需求管理是CMM可重复级中的6个关键过程域之一,其主要目标是_________ ...
- python flask学习(1)与Git基础操作
今天从简单的flask开始完成Flask web开发的学习.今天学习了Git和GitHub项目的提交. Git尝试提交过程中出现了"Could not read from remote re ...
- dispaly:none 和visibility :hidden的区别
display:none 通常被 JavaScript 用来在不删除元素的情况下隐藏或显示元素. 它和 visibility 属性不一样.把 display 设置成 none 元素不会占据它本来应该显 ...