Delegate的Target,Method
在 C# 中,Delegate 是一种引用方法的类型,可以将方法视为对象进行传递和操作。Delegate 类型的实例可以用来引用一个或多个方法,然后可以将这些引用作为参数传递给其他方法,或者用来调用这些方法。
Delegate 类型包含两个属性:Target 和 Method。其中,Target 属性表示委托引用的对象,Method 属性表示委托引用的方法(MethodInfo),当调用多播委托时,它会依次调用每个方法。在这种情况下,Target 属性返回委托引用的最后一个方法的对象,如果是静态方法,则为null。
public class TestA
{
public void print()
{
Console.WriteLine("this is testA");
}
public virtual void printH()
{
Console.WriteLine("this is testH in A");
}
}
public class TestB : TestA
{
public static void StaticMethod()
{
Console.WriteLine("this is static method of TestB");
}
public new void print()
{
Console.WriteLine("this is testB");
}
public override void printH()
{
Console.WriteLine("this is testH in B");
}
}
class ProgramA
{
static void Main()
{
Action actA = null,actB=null,actC=null;
var a = new TestA();
var b = new TestB();
actA += a.print;
actA += a.printH;
Console.WriteLine(actA.Target==a);
Console.WriteLine(actA.Method==typeof(TestA).GetMethod("printH"));
actB += b.print;
actB += b.printH;
actB += TestB.StaticMethod;
Console.WriteLine(actB.Target==null);
Console.WriteLine(actB.Method==typeof(TestB).GetMethod("StaticMethod"));
actC = actA + actB;
var c = actC.GetInvocationList();
Console.WriteLine("c length:"+c.Length);
foreach(Delegate d in c)
{
Console.WriteLine($"Target:{d.Target},MethodInfo:{d.Method}");
}
Console.ReadLine();
}
}
output:
True
True
True
True
c length:5
Target:EasyBimBackend.TestA,MethodInfo:Void print()
Target:EasyBimBackend.TestA,MethodInfo:Void printH()
Target:EasyBimBackend.TestB,MethodInfo:Void print()
Target:EasyBimBackend.TestB,MethodInfo:Void printH()
Target:,MethodInfo:Void StaticMethod()
Delegate的Target,Method的更多相关文章
- C#内存泄漏--event内存泄漏
内存泄漏是指:当一块内存被分配后,被丢弃,没有任何实例指针指向这块内存, 并且这块内存不会被GC视为垃圾进行回收.这块内存会一直存在,直到程序退出.C#是托管型代码,其内存的分配和释放都是由CLR负责 ...
- 手写Koa.js源码
用Node.js写一个web服务器,我前面已经写过两篇文章了: 第一篇是不使用任何框架也能搭建一个web服务器,主要是熟悉Node.js原生API的使用:使用Node.js原生API写一个web服务器 ...
- Delegate, Method as Parameter.
代理, 将方法作为另一方法的参数. 类似C里面的函数指针. using System; using System.Windows.Forms; using System.Threading; name ...
- C# delegate multicast single delegate
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serializatio ...
- Delegate、Predicate、Action和Func
写在前面 Delegate Predicate Action Func 逆变和协变 先说下什么是委托(Delegate),委托在C#中是一种类型,和Class是一个级别,但是我们经常把它看做是一个方法 ...
- C#高级编程笔记 Delegate 的粗浅理解 2016年9月 13日
Delegate [重中之重] 委托 定义一:(参考)http://www.cnblogs.com/zhangchenliang/archive/2012/09/19/2694430.html 完全可 ...
- 如何重载delegate
在写delegate的时候遇到一个问题,在已有一个不带参数的delegate基础上,试图再增加一个带参数的delegate,结果VS报了“already contains a definition f ...
- Aspectj 实现Method条件运行
最近我花了半个小时实现了一个Method的按自定义条件运行的plugin,Condition-Run.实现场景是由于我所工作的客户经常会是在同一个代码集上实现多个Brand,所以有些功能只会限制是几个 ...
- c# 关键字delegate、event(委托与事件)[MSDN原文摘录][1]
A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. ...
- 谈C#中的Delegate
引言 Delegate是Dotnet1.0的时候已经存在的特性了,但由于在实际工作中一直没有机会使用Delegate这个特性,所以一直没有对它作整理.这两天,我再度翻阅了一些关于Delegate的资料 ...
随机推荐
- Linux - top相关的快捷键
q:退出top命令窗口(quit). k:按照进程ID终止(kill)一个进程.例如,你可以输入k,然后输入进程的PID来终止它. r:重新设置进程的优先级.输入r后,你可以输入新的优先级值. f:进 ...
- ABC393E题解
大概评级:绿. 拿到题目,寻找突破口,发现 \(A_i \le 10^6\),一般的数据都是 \(A_i \le 10^9\),所以必有蹊跷. 数学,权值,最大公约数,联想到了因子--懂了,原来是这么 ...
- 什么是git,什么是github,git和github的使用
Git实战 注意:本项目是学习笔记,来自于哔哩哔哩武沛齐老师的Git实战视频, 网址:[武沛齐老师讲git,看完绝对上瘾!!!] https://www.bilibili.com/video/BV1n ...
- JSON驱动的vue可视化表单设计器组件
form-create-designer 是基于 @form-create/element-ui 实现的表单设计器组件.可以通过拖拽的方式快速创建表单,轻松帮你搞定表单. 源码地址: Github | ...
- 【技术分析】EIP-7702 场景下 EOA 授权签名的安全探讨
EIP-7702 在 2025 年即将到来的以太坊 Pectra 升级中,将会引入 EIP-7702 这个提案.其主要的内容就是使得 EOA 账户拥有了自己的 Storage ,并且可以通过 dele ...
- PV、UV、VV、IP含义及计算方式
什么是PV? PV 即 Page View,网站浏览量,指页面浏览的次数,用以衡量网站用户访问的网页数量. 用户每次打开一个页面便记录1次PV,多次打开同一页面则浏览量累计.一般来说,PV与来访者的数 ...
- 0003 Failed to build the application: build go_beego/src/hello: cannot load
我使用beego框架快速建立了一个应用,可当我运行 bee run的时候,出现了如下错误 D:\go_beego\src\product>bee run ______ | ___ \ | |_/ ...
- 【ffmpeg】avformat_alloc_context报错System.NotSupportedException不支持所指定的方法
这个错误报了第二次了,网上搜不到靠谱的解决方案,赶快记录一下. 第一个情况:报错如题目System.NotSupportedException 不支持所指定的方法 第二个情况:如果换autogen版本 ...
- MFC非模态对话框的关闭
如果要在点击按钮的情况下,销毁非模态对话框,只需要把按钮的事件映射到OnCancel函数, 里面调用DestroyWindow(), 然后重写PostNCDestroy(), delete 指针. 另 ...
- 安装调用.so文件
博客地址:https://www.cnblogs.com/zylyehuo/ 使用 pwd 命令找到 .so 文件 首先使用 pwd 命令找到要安装的 .so 文件.通过使用此命令打印当前工作目录来找 ...