Skip to main content

C# 4.0 dynamic Objects-Part-I


 C# 4.0 dynamic Objects

Revisiting the C# 4.0 article (see my earlier post), new features introduced in C# 4.o can be grouped under the following

Dynamic binding
Named and optional arguments
Variance

Today I’ll discuss the most interesting feature the “Dynamic Programming”.


C# 4.0 introduces a new static type called dynamic (under using System.Dynamic namespace)

Dynamic objects expose members such as properties and methods at run time rather than at compile time.
At compile time the dynamic object is assumed (no static binding) to support any specified operation and it is only at runtime that the error will crop up if the concerned operation is not supported.

The type dynamic can be considered as a special version of the object type. Any object can be implicitly converted to dynamic with type checking postponed until runtime. Conversely, expressions of type dynamic can be implicitly converted to object, or any other type, as long as there exists a conversion at runtime.

For e.g.
dynamic d = 25; compile-time implicit conversion
int pp = d;     runtime implicit conversion
Variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.

Dynamic operations

Method calls along with field and property accesses, indexer and operator calls ,delegate invocations and constructor calls can be dispatched dynamically:
dynamic d = GetDynamicObject(…);
d.M(7);              calling methods
d.f = d.P;           getting and settings fields and properties
d[“one”] = d[“two”]; getting and setting through indexers
int i = d + 3;       calling operators
string s = d(5,7);   invoking as a delegate
var c = new C(d);    calling a constructor
The role of the C# compiler here is simply to package up the necessary information about the dynamic object d, so that the runtime can pick it up and determine  needs to be done with object d.


Dynamic functionality is greatly enhanced by DLR (Dynamic Language runtime) and in fact, has been made possible because of DLR (for brief into of DLR see my earlier posts).

Part-II of the series will actually concentrate on some detailed aspects of Dynamic binding wherein I’ll cover the dynamic functionality with an elaborated example.

Hope this was Helpful.

Till next time we connect, Happy Coding!!

Comments

Popular posts from this blog

Asp.Net 4.0: An Overview-Part-III

This is the last post in the series which will explore the following new features of ASP.Net 4.0  Performance Monitoring for Individual Applications in a Single Worker Process Web.config File Refactoring Permanently Redirecting a Page Expanding the Range of Allowable URLs Performance Monitoring for Individual Applications in a Single Worker Process It is a common practice to host multiple ASP.NET applications in a single worker process, In order to increase the number of Web sites that can be hosted on a single server. This practice results in difficulties for server administrators to identify an individual application that is experiencing problems. ASP.NET 4 introduces new resource-monitoring functionality introduced by the CLR. To enable this functionality, following XML configuration snippet is added to the aspnet.config configuration file.(This file is located in the directory where the .NET Framework is installed ) <?xml version="1.0" encoding="UTF-8"

WCF-REST Services-Part-II

HOW REST is implemented in WCF Part-I of the series explored the REST conceptually and this post will explore how REST is implemented in WCF. For REST implementation in WCF, 2 new attributes namely WebGetAttribute and WebInvokeAttribute are introduced in WCF along with a URI template mechanism that enables you to declare the URI and verb to which each method is going to respond. The infrastructure comes in the form of a binding ( WebHttpBinding ) and a behavior ( WebHttpBehavior ) that provide the correct networking stack for using REST. Also, there is some hosting infrastructure help from a custom Service¬Host ( WebServiceHost ) and a ServiceHostFactory ( WebServiceHostFactory ). How WCF Routes messages WCF routes network messages to methods on instances of the classes defined as implementations of the service. Default behavior ( Dispatching ) for WCF is to do this routing based on the concept of action. For this dispatching to work, an action needs to be present in ev

SOLID principles -Code Samples and Free Ebook

I planned to write code samples for SOLID principle implementations, however I am a firm believer of " NOT RE-INVENTING THE WHEEL ", when all you need is use the wheels and make a new CAR. Going by the ideology, I have found an excellent  SOLID principles FREE -Ebook ( covering all aspects of SOLID design principles, with Code sample). This book is an excellent visual aid to remember these principles, as it uses Motivational posters for explaining SOLID design principles. One additional advantage to the above mentioned book is the Code-Refactoring ebook . Both of these books can be downloaded from this EBOOK download Link Both of these books can be downloaded form here. Hope this book proves useful... Till next we connect.... Happy Learning..