Xpode.com        Click here to Print this article.

.NET interview questions: - What are practical uses of Dynamic and reflection keyword?

This is the .NET interview questions asked to candidate to judge his practical skills. We have tried to put the same in simple words as follows: -

Practical uses of Dynamic: -

One of the biggest practical uses of dynamic keyword is when we operate on MS office components via interop.

So for example if we are accessing Microsoft excel components without dynamic keyword, you can see how complicated the below code is. Lots of casting happening in the below code,  right.

// Before the introduction of dynamic.

Application excelApplication = new  Application();

((Excel.Range)excelApp.Cells[1, 1]).Value2 = "Name";

Excel.Range range2008 = (Excel.Range)excelApp.Cells[1, 1];


Now look at how simple the code becomes by using the dynamic keyword. No casting needed and during runtime type checking also happens.

// After the introduction of dynamic, the access to the Value property and 

// the conversion to Excel.Range are handled by the run-time COM binder.

dynamic excelApp = new Application();

excelApp.Cells[1, 1].Value = "Name";

Excel.Range range2010 = excelApp.Cells[1, 1];

Practical uses of Reflection: -

  • If you are creating application like visual studio editors where you want show internal of an object by using intellisense.
  • In unit testing sometimes we need to invoke private methods. That’s a different thing test private members are proper or not.
  • Sometimes we would like to dump properties, methods and assembly references to a file or probably show it on a screen.












http://
http://questpond.com/

Contributed by:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers

Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=714

Click here to go on website