Delegates internals
来源:互联网 发布:数据建模分析 工具 编辑:程序博客网 时间:2024/06/05 07:34
I was just digging inside the delegates, action and anonymous delegates via lambda expression. I started to ask a few curious questions to myself and thus set out to dig inside to answer them. While doing so, I found a few interesting things which I would like to share with you all. Hope it is useful:
First I wrote a sample code as shown below:
Action del = () => Console.WriteLine(“Hello”);del();del.Invoke();
Well yes, the code is very simple, but I started to wonder and asked a question myself, what is the difference between the above two different styles of delegate invocation.
If in case you’re not aware of it, every delegate internally is represented as a class type having just four important methods: BeginInvoke
, Invoke
, EndInvoke
, and Ctor
. There is nothing much to explain about these methods.
I looked inside the IL and came to know that the above two styles of delegate invocation is in fact the same. The compiler just converts del()
to del.Invoke()
. So there will be twodel.Invoke()
statements as shown in the below IL opcodes:
IL_0022: callvirt instance void [mscorlib]System.Action::Invoke()IL_0027: nopIL_0028: ldloc.0IL_0029: callvirt instance void [mscorlib]System.Action::Invoke()
Next I asked myself how lambda expressions used above are treated internally. I found out that the lambda expression which I was using in the code is treated as a class internally in the IL. The below code produced two classes for the anonymous delegates I used:
MyDelegate del = () => (2 + 2).ToString();del = () => (4 + 4).ToString();\
Let's look into the IL and see what charm the compiler has done:
// Fields.field private static class ConsoleApplication.Program/MyDelegate ‘CS$<>9__CachedAnonymousMethodDelegate2′.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (01 00 00 00).field private static class ConsoleApplication.Program/MyDelegate ‘CS$<>9__CachedAnonymousMethodDelegate3′.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (01 00 00 00)
As you can see, it has produced two classes for each of those anonymous delegates with the name shown above. No matter where the anonymous delegates are declared, they do get the same naming styles. But I still wonder and look for an answer why all anonymous delegates are prefixed with CS$<>9__
. May be Microsoft chose to keep it that way? No idea! If you know, kindly leave a comment.
That’s all I could find so far, I shall surely share more if I find out more
Thanks
- Delegates internals
- 8.10 Delegates
- 22. Delegates
- Java delegates
- PsExec Internals
- QEMU Internals
- Spy++ Internals
- GDB Internals
- gcc internals
- android internals
- NSRunLoop Internals
- WebRTC Internals
- Spring Internals
- MySQL Internals
- golang internals
- Mmap Internals
- R Internals
- Puppet Internals
- 一些产品的思路-张小龙分享
- 3、各种OP放大器选型手册
- vector/list/map
- SerializationException: 在分析完成之前就遇到流结尾
- LDO和DCDC电源的区别 .
- Delegates internals
- 模态对话框和非模态对话框的区别
- 如何配置FCK编辑器图片上传
- linux驱动入门---test驱动模块加载
- 函数指针及其用作函数返回值
- 百炼 2980 大整数乘法
- Windows的“分辨率”与“DPI”
- sql如何批量删除字符串中的前三个字符
- 学习笔记(无聊时的总结)