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的资料 ...
随机推荐
- 对接服务升级后仅支持tls1.2,jdk1.7默认使用tls1.0,导致调用失败
背景 如标题所说,我手里维护了一个重要的老项目,使用jdk1.7,里面对接了很多个第三方服务,协议多种多样,其中涉及http/https的,调用方式也是五花八门,比如:commons-httpclie ...
- phpinclude-labs做题记录
Level 1 file协议 payload:?wrappers=/flag Level 2 data协议 去包含data协议中的内容其实相当于进行了一次远程包含,所以data协议的利用条件需要 ph ...
- Suspense和vue-async-manager
Suspense Suspense是 Vue3.x 中新增的特性, 那它有什么用呢?别急,我们通过 Vue2.x 中的一些场景来认识它的作用. Vue2.x 中应该经常遇到这样的场景: <tem ...
- games101 作业4提高部分
games101 作业4提高部分 作业四中,我们按照实验步骤完成bazier曲线之后,得到的结果有一定的锯齿感: 然后pdf中给出的思路是: 对于一个曲线上的点,不只把它对应于一个像素,你需要根据到像 ...
- CS硕士全日制考研资料(含完整复习计划)
择校信息 华东师范 2021招生专业考试科目:https://yjszs.ecnu.edu.cn/system/sszszyml_list.asp 计算机科学与技术:https://yjszs.ecn ...
- 接口中的成员特点、类和接口之间的各种关系--java进阶day02
1.接口的成员特点 1.接口没有构造方法 接口没有构造方法,但是实现类中有构造方法,super()又该访问谁呢? 类实现接口只是认干爹,类本身还是会有亲爹Object,super()会访问Object ...
- 【Guava】BiMap&Multimap&Multiset
BiMap Map 可以实现 key -> value 的映射,如果想要 value -> key 的映射,就需要定义两个 Map,并且同步更新,很不优雅.Guava 提供了 BiMap ...
- langchain0.3教程:聊天机器人进阶之方法调用
我们思考一个问题:大语言模型是否能帮我们做更多的事情,比如帮我们发送邮件.默认情况下让大模型帮我们发送邮件,大模型会这样回复我们: 可以看到,大模型无法发送邮件,它只会帮我们生成一个邮件模板,然后让我 ...
- 格林威治时间(Tue Jan 01 00:00:00 CST 2019)转Date
Excel导入时后台接受日期格式数据为[格林威治时间](例:Tue Jan 01 00:00:00 CST 2019) 格林威治时间转Date package com.cn; import java. ...
- Ubuntu v22配置用户临界值
方法 1:使用 pam_faillock(推荐,Ubuntu 22.04 默认方式) pam_faillock 是较新的 PAM 模块,用于记录失败登录尝试并在达到限制后锁定账户. 修改 /etc/p ...