Friday, August 10, 2012

.Net Framework – A Layman’s View (Part -2)


Hi All,

It is part-2 of our .net framework discussion. I felt myself this post is a boring one as it is pure theory but you should know about this being a .net programmer.

Shall we start?

I think if you are following my posts from the beginning, you might have understood that I like to ask questions. In my school days, I used to feel like if the teacher doesn’t know anything about the subject, he manages it by posting questions to the students. Please do not treat me in the same way.

Anyway here is our conversation. I’m just assuming your responses and writing. I’m also assuming that you are new to .net programming world.

Me: Please consider the below program. Can you write summary of the program in a word document?


  

You: What should I write about the program?
Me: You need to write list of methods/ functions, properties.

You: Ok here it is.

  
Me: Also, Please create one separate class like this.


You: Created.

Me: Please add this class to your first class.
You: Added. Here it is. Just have a look. Is it correct?


Me: Perfect. We will add a call to this class in our program.
You: Ok.




Me: Fine. In the beginning you have created a table of types and methods your program is referring correct?
You: Yes.

Me: Can you update that table with the new type and methods you have added?
You: Ok. Here it is.

Me: Please write all these tables in a notepad and save.
You: Ok saved.

Me: Which name did you give to that file?
You: ObjetList.txt

Me: Give some nice name buddy.
You: What name you think is nice ?

Me: Metadata.
You: Metadata ? Ok all this nonsense you told so far to explain me metadata?

Me: ??????
Me: Nonsense ???

Again  Me: Ok. I will continue the nonsense. In the previous post I said when you click on build project, your .net compiler produced .exe or .dll correct ?
You: Yes. But I did not see any text file there.

Me: No just for your understanding I told like that. Your .net exe or .net dll does consist of above tables data also.  It is called metadata.
You: Then what is manifest ?
Me: You said nonsense?
You: Just for fun. It is my casual punch dialogue.

Me: Never say this to girls.In this example, how many projects did you add to your solution?
You: One only.

Me: If you add two/three projects?
You: If I add ????

Me: If you build, it will generate 2,3 exe’s or dll’s.
You: Ok. Then ?

Me: This will have 2,3 metadata’s correct ?
You: Correct. So…. ?

Me: If I ask you to write list of these 2,3 metadata’s in a separate table, it is called manifest.
You: That means, if only one project is there in the solution then manifest won’t be there. Correct?

Me: Wrong. It will be there with one entry in the table.

Ok guys/gals our discussion closed. I know what you are thinking. Just to give two lines of definitions about metadata and manifest, this fellow wasted 2 pages. Forgive me for this. 

Anyways, I would like to ask you one more question.

In the previous post, I said our compiler produce MSIL. But when this will be converted to machine understandable language?

Let us consider our same example given above. When we press F5, will the whole exe gets converted into 0’s and 1’s ?

Answer is “No”.

Here are the steps this particular example passes through.

  1. Press F5 to run the program 
  2. CLR enters Main method

3. CLR checks what types (dll’s) and methods of those types used in the Main method and stores them like below.





Here Beep method is there in a class called “Console”. Our CLR will not convert Beep IL code into 0’s and 1’s immediately. It is done only when it is called. But it maintains a structure of types and methods this particular method is referring. In this structure, address of Beep method is blank as it is not yet converted from MSIL to 0’s1’s 

      4. Now, Beep method called


      
      5. CLR checks if this method is already converted to 0’s and 1’s. It is not.  That means machine readable form of Beep method is not available.
      6. It goes to the Console dll and gets MSIL of the Beep method
      7. Converts this MSIL of Beep into machine readable language and stores address of that in the structure created above.


1.    


             8. Steps 6 and 7 are performed by one function in the CLR called JIT compiler
      9. Now Beep (999,99)method called


      10. CLR checks in the data structure table if Beep method already converted. 
       
       It is !!

       So JIT compiler will not be called. CLR just takes this address and executes it. This is the reason when you run your .net programs; it will be slow first time and better for the subsequent runs as this conversion process will be skipped. 

      Of course I think you understood from above explanation that, CLR coverts MSIL to 0’s1’s dynamically and store them in the memory when the program is running. If you stop your application then obviously memory will be cleared. If you press F5 again, process will start from step 1.

These same steps will be repeated for all the method during execution of the program.

I hope it is clear to you.

I’m signing off now

No comments:

Post a Comment