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

WPF Routing

WPF (3.5) introduced the concept of Routing that made the event routing easies in the scenarios where it was tedious to handle events. Consider a scenario where there are a number of Hyperlinks in a Panel that direct to separate locations on Click. Now if this is done in normal programming, each hyperlink will have to have code for execution. It would be easier and cleaner if we could handle the hyperlinks in the container (the Panel) that handles the click and redirects to appropriate location. WPF handles the events with the following 3 strategies. Direct events are like ordinary .NET events. They originate in one element and don’t pass to any other. For example, MouseEnter is a direct event. Bubbling events are events that travel up the containment hierarchy. For example, MouseDown is a bubbling event. It is raised first by the element that is clicked. Next, it is raised by that element’s parent, and then by that element’s parent, and so on, until WPF reaches the top of the e...

WPF Overview-Part-II

This post is in continuation to the last post. In this Post I’ll be exploring the Dependency Properties Dependency properties are similar to CLR properties with more advanced and complex features. The main difference between the CLR properties and dependency properties is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject . In case this description did not make sense, no need to worry, It will become clear by the time you reach end of this article. How the Value is Resolved in Dependency properties Every time a dependency property is accessed, it internally resolves the value by following the precedence from high to low. It checks if a local value is available, if not, check if a custom style trigger is active and I the similar manner continues until it finds a value. At last the default value is alwa...