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.