Dynamic Type with Reflection in debug mode in .NET

No.of Views576
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  09 Apr 2010 08:04:29
Tag : .NET Frameworks , Debug and BreakPoints
This article will give simple debug mode explanation on how Reflection and Dynamic type works.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will give simple debug mode explanation on how Reflection and Dynamic type works.

Note: Read my previous article on dynamic type here Dynamic Type Let us create a very simple class called Student.

public  class Student
    {
       
       public void Print(string Name)
       {

           Console.WriteLine("Student Name is " + Name);

       }

    }


Now say, you don’t have information about type of the class while creating instance of this class.  Or in other words you don’t have type information of the class at the compile time.  So how to create instance of this class? Till c# 3.0 ; we had no choice but to take help of Reflection . Code was highly messy. Not very readable and not beautiful either. 
 

Code will look like,

Object student = new Student();
Type studentType = student.GetType();
 Object res = studentType.InvokeMember("Print", BindingFlags.InvokeMethod,null, student ,new Object[] {"Dhananjay"});
 Console.Read();

Few points about above code

1.    We are creating instance of the Student class as object type. Since we don’t know type of the class at compile time.

 

Image Loading

By running in debug mode, we can see at compile time class is resolving as DynamicReflection.Student.


2.    In 2nd line of code we are finding, type using GetType method of object class.
 

Image Loading

  We can see Compiler is resolving the type as Student.  If you remember Student is name of our class.
3.    On the type (StudentType in our case) we are calling the InvokeMember method to call the method on the instance of the class.  InvokeMember is overloaded function and we are passing four parameters to this.
4.    Since method of the class is returning void so res is NULL.

Sample Output
 

Image Loading

Now if you see above code, is not it very messy?  So in c# 4.0 we do have dynamic type to deal with.

dynamic dynamicStudent = new Student();
            dynamicStudent.Print("Dhananjay With Dynamimc Type ");
            Console.Read();


Now again if you run the above code in debug mode, you will see  type for dynamic type is getting resolved exactly the same as of reflection.   

Image Loading

After type resolving all the other work to call method is done by compiler at the back ground.  So simply we need to call the method on the dynamic type and DLR will take care of everything.

 

Image Loading


So, we saw in debug mode how dynamic type and reflection works.  Thanks for reading.
 

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    Let us start this article by a small chat between customer and developer. Scenario 1 Customer: - How’s your application performance? Subjective developer: - Well it’s speedy, it’s the best …huuh aaa ooh it’s a like rocket. Scenario 2 Customer: - How’s your application performance? Quantitative developer: - With 2 GB RAM , xyz processor and 20000 customer records the customer screen load in 20 secs. I am sure the second developer looks more promising than the first developer. In this
    Published Date : 10/May/2010
    Ask any developer which is the best place in a .NET class to clean unmanaged resources?, 70% of them will say the destructor. Although it looks the most promising place for cleanup it has a huge impact on performance and memory consumption. Writing clean up code in the destructor leads to double GC visits and thus affecting the performance multifold times. In order to validate the same we will first start with bit of theory and then we will actually see how GC algorithm performance is impacte
    Published Date : 06/May/2010
    One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET.
    Published Date : 05/May/2010
    This article will explain 6 important concepts Stack , heap , by val , by ref , boxing and unboxing. This article starts first explaining what happens internally when you declare a variable and then i
    Published Date : 02/May/2010
    Introduction Value Types in .Net Framework
    Published Date : 09/Apr/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top