Filip is an enthusiastic developer that strives to learn something new every day. His greatest passion is programming and ever since Filip was a little boy he has always strived to master it. All his community contributions has made Filip a Microsoft Visual C# MVP and a DZone Most-Valuable Blogger.
Compiling C# In this first module, we will look at what happens when we compile our C# application and what is generated from its compilation. Now, we'll start off by talking about what the compiler is. So, essentially, the compiler is something that translates a chunk of data into something else. The C# compiler doesn't generate any machine code. This means that the code isn't executed directly on the CPU in the system, but there's a runtime called the CLR, or the Common Language Runtime, that interprets the code that is compiled by the C# compiler and executes that. So at some point, the code that is generated by the compiler will actually be converted into machine code. So let's say that we have this C# snippet that adds 10 to 20, and when we compile this, the code that we see on your right-hand side here is what is generated. Don't worry if you don't understand the code that is generated by the compiler. This is what we are going to dig deeper into.
Understanding IL and the Evaluation Stack In the first module, we discovered how we could find the MSIL that was generated when we compiled our C# applications and we had a brief look at what this MSIL looked like. In this module, we're going to have a more thorough look at the MSIL that is generated from compiling a C# application and we're going to go over the basic operation codes used in those snippets. And we're also going to explore the Evaluation Stack and see how that behaves when we execute our operations. We've had a look at the structure of an MSIL code snippet and we've discovered that we can define labels in the snippets that we use when we want to transfer control. Transferring control is often done when we do an if statement, or null statements, a switch case, or anything like that. And we're going to take a look at the most common ways to transfer control later in this module. We've also discovered that we execute operations in MSIL by emitting something called OpCodes, and we're going to explore these OpCodes in this module. And then we have this stack, which is fundamental to understand if we want to understand MSIL. The stack is used to process information used by our operations, and we're going to dig deep and understand the stack and what happens when an MSIL operation is executed.
Building things at runtime In the following two modules, we're going to look at how we can build things in runtime. We're going to use the knowledge that we've gathered throughout the course and see how we can utilize that to actually create things in runtime. We're going to do this by using Reflection together with IL. So what is Reflection? I usually say that this is what you get when you look into a mirror. You're going to see a reflection of yourself. And when you see a reflection of yourself, you can see all the good parts and maybe some bad parts, and you can see things that you might want to change and things that you might want to add, such as some more makeup or some more gel in your hair. So how does this apply to code? Well, we can actually do the exact same thing here. We can use Reflection to get information about the code that is running. So we can use Reflection to get information in runtime, such as information about the class that is currently executing or methods inside another class. We can use Reflection to add behavior. Just as we can add more gel to our hair or add some more makeup, we can add more things in runtime using Reflection in C#.
Building Things at Runtime Part 2 In this module, we're going to continue looking at how we can build things in runtime using MSIL together with the DynamicMethod. And we're going to continue trying to convert C# methods into MSIL to get an understanding of what happens when we compile our C# applications.
Understanding the Different OpCodes Throughout the course we've looked at how we can use MSIL inside our C# applications and we've discovered a lot of common operation codes that we've seen frequently being used in almost all the examples. Now let's try to take a look at some more operation codes and try to see where these operation codes might apply.