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...

Covariance and Contravariance-General Discussion

If you have just started the exploration of .Net Framework 4.0, two terms namely Covariance and Contravariance might have been heard. The concept that these terms encapsulate are used by most developer almost daily, however there has never been any botheration about the terminologies. Now, what actually these terms mean and how are these going to affect us as a developer, if we dive in to the details. The simple answer is it’s always good to know your tools before actually using them. Enough philosophy, let’s get to the business. Starting the discussion let me reiterate that in addition to Covariance and Contravariance, there is another terminology, Invariance. I’ll by start here by diving into the details of Invariance and then proceed further. Invariance: Invariance can be better understood by considering the types in .Net.>net has basically two type, value-types and reference-types. Value types (int, double etc) are invariant i.e. the types can’t be interchanged either ...

Advanced WCF

In this post, I am sharing the link of articles about  advanced topics in WCF. The List of articles is exhaustive and can serve as your repository for all WCF queries. Concurrency,Throttling & Callbacks  WCF Concurrency (Single, Multiple and Re entrant) and Throttling   WCF-Interop and BinarySecurityToken  WCF Callbacks  Creating Web Services From WSDL Link1 Link2 Link3 Link4 WCF-Security WCF over HTTPS   Transport Security(basic)/HTTPS UserNamePasswordValidator ServerCertificateValidationCallback 9 simple steps to enable X.509 certificates on WCF - CodeProject http://www.codeproject.com/KB/WCF/9StepsWCF.aspx?display=Print Message Security(Certificate)/PeerTrust Securing WCF Services with Certificates. - CodeProject http://www.codeproject.com/KB/WCF/wcf_certificates.aspx Message Security(Certificate)/ChainTrust How To Configure WCF Security Using Only X.509 Certificates - CodePr...