I am learning .NET/C# and because I almost always want to find out how managed object represent in real开发者_运维问答 memory, I am wondering if there is any programmatic method to get these information, including object memory address, size in byte, memory layout and so on?
I know that .NET discourage it and hide information as internal implementation detail. But I am really curious about it for debugging and learning purpose.
There are 2 compiled versions of the code that is produced the C# compiler:
- First, it is compiled into Common Intermediary Language (CIL). You can look at this by using the
ildasm
command in a Visual Studio Command Prompt or by using a (better) tool like Redgate's .NET Reflector - It is then compiled from CIL to native machine code (by the Just In Time Compiler) and run. You can view this code by going
Debug > Windows > Disassembly
in Visual Studio.
But you are interested in the memory representation of the native types. This may be difficult and hard to grasp, but if you feel like you're up to it you can use WinDbg
and SOS.dll
to debug Managed Code in a native way. I would look at this list of links to get you started. Just google "SOS.dll" for more information.
I don't think you will be able to (easily) do this programatically at all from the CLR. Maybe (big maybe) if you used NGEN to compile your assemblies ahead-of-time then use some sneaky C or C++ you might be able to do this, but it would be extremely hard (this is out of my knowledge zone, someone who works on the JIT team might be able to help you)
精彩评论