New Features of C# 4.0: Dynamic LookupDCI
This blog is just a continuation of our previous article New Features of C# 4.0 in which we just listed out some of the new features available on C# 4.0 with a brief description of each. To gain more knowledge about significance of each of those features, we have planned a series of posts on C# and this is the first post of the series.
For this, let us start with the topic Dynamic Lookup. The reason for this start is because dynamic lookup is not a new thing to hear for .NET community people because it has been introduced in its Common Language Runtime (CLR) but termed as late binding.
Please note that we had already provided with a simple snippet example for this too that tells how to do dynamic lookup using the keyword dynamic.
The main thing to note about this keyword dynamic is that whenever C# 4.0 compiler encounters the keyword, it just ignores the static checking and resolves it to do at runtime. The next thing that happens is that is at runtime after firing up of the reflector, a new code is generated that acts as a container for the dynamic code.
Actually, C# 4.0 provides an extended facility in dynamic lookup by providing a shared infrastructure in the form of Dynamic Lookup Runtime (DLR) that comes packed with Visual Studio 2010. DLR provides a common infrastructure for .NET languages that helps developers to build a common set of tools using any language that has the support for DLR. By this, .NET can share the libraries and COM objects developed in DLR languages and vice versa.
Difference between CLR Late Binding and Dynamic Lookup introduced in the C# 4.0
By late binding in C#, we mean that our code can recognize the type of the COM object at runtime by calling GetTypeFromProgID method and then create an instance of that object using CreateInstance method through reflection rather than using Runtime Callable Wrapper (RCW) that is used in case of early binding references. C# 4.0 goes even beyond by way of dynamic lookup. There are actually three ways of creating and using dynamic lookup namely,
- First, if the object is of COM type, then operation is dispatched through COM IDispatch.
- Second, in view of providing more interoperability for the COM objects created using DLR languages, dynamic objects in C# 4.0 can implement the IDynamicObject interface and completely redefine the meaning of dynamic implementation. This method is very much similar to the methods implemented by dynamic languages IronPython and IronRuby that implements their own dynamic models.
- Third, if the object is of plain C# object, then late binding takes into effect through simple reflection mechanism.
DLR with CLR
The DLR interoperates with our C# is through the namespace Microsoft.CSharp.RuntimeBinder that provides infrastructure to resolve the dynamic objects developed using dynamic languages from our C# code by way of its classes and enumerations. We can refer to the official documentation of Microsoft on this to gain more knowledge about the methods and properties it provides for dynamic lookup.
How Dynamic Lookup is Implemented ?
C# 4.0 actually introduces a new pseudo-type called dynamic which is treated as System.Object. Through this pseudo-type, any members of this dynamic object like fields, properties, methods, indexes, or delegates can be invoked dynamically without actual type checking made at compile time. This way of invoking feature is most commonly called duck typing. In case of duck-typing, a function call can be made of an object of any type and if the actual function does not exist for that object, it will throw runtime exception.
Issues Related with this Duck-typing Dynamic Object
The burden is put on the programmers’ side because he/she needs to have a thorough knowledge on which method is coded for what purpose because the same method named in two different objects, coded for different purposes, may produce adverse effects at runtime that would be hard to detect and rectify.
Dynamic Lookup: A Boon or a Ban?
To conclude, Microsoft provides more capabilities and enhancements on code interoperability by way of dynamic lookup and it is the responsibility of the programmers to carefully study and analyze before developing a dynamic object and hard-code the functions and behaviors.