Dynamic type in c# 4.0

Objective


This article will introduce dynamic type. This article will be giving samples on different usage of dynamic type in programming.

Prerequisite: Visual Studio 2010

C# 4.0 is all about Dynamic Programming and type.


 10 facts about Dynamic type

  1. C# 4.0 allows a new static type called “dynamic”.
  2. Any operation on the object of type dynamic resolve at runtime.
  3. The dynamic allows us to access the object, without knowing type of the object at compile time.
  4. Dynamic types are internally treated as System.Object.
  5. At compile time we declare a dynamic type but at run time we get a strongly typed object.
  6. Dynamic type gets escaped from compile type checking. But if any error, it caught at runtime.
  7. Dynamic type is assumed to perform any task at compile time.
  8. Variable of type dynamic compiled into type object. So, after compilation dynamic does not exist.
  9. Dynamic type does not support intelli-sense.
  10. Object of any language type could be treated as dynamic type provided they are capable of naturally dispatch them.


 Usage


This is used to call and consume object of dynamic type languages like Python and Ruby. Now C# could call object of any dynamic language in the type dynamic.

Sample 1

 

Program.cs

using System;

 using System.Collections.Generic;
using System.Linq;

using System.Text;

namespace DynamicTypeSample1

{


class
Program

{static void Main(string[] args)

 

{


dynamic dobj = “Hi I am Dynamic “;


Console.WriteLine(dobj);


Console.ReadKey(true);

}

}

}

Output

Explanation

  1. In above code, I am defining a variable dobj of type dynamic.
  2. This type is resolved in type string at runtime.
  3. We are printing the string value.

To get the runtime type of the dynamic type, I am adding one line of code in above code as

class
Program

{


static
void Main(string[] args)

{


dynamic dobj = “Hi I am Dynamic “;


Type dobjtype = dobj.GetType();


Console.WriteLine(dobjtype);


Console.WriteLine(dobj);


Console.ReadKey(true);

}

}

Output


Explanation

  1. GetType() operation on dynamic type is use to get the runtime type of the dynamic type.
  2. As output, we are getting System.String. This is runtime type of the dynamic type.

Dynamic type could be used with

  1. Local type.
  2. Function return type
  3. Properties
  4. Indexers
  5. Class type
  6. Function argument
  7. Type constraint.

Conversion on dynamic type

  1. Any object type can be converted into dynamic type implicitly.

dynamic dobj = 9;


dynamic dobj1 = ” Dhananjay Kumar”;


dynamic dobj3 = System.DateTime.Today;

  1. Dynamic type could also get converted into any type implicitly

int i = dobj;


string s = dobj1;


string sd = dobj3;

Sample 2

In this sample

  1. We will make a class SampleForDynamimc.
  2. There are two properties, one is of type string and another is of type dynamic.
  3. There is one method, and return type of this method is dynamic and it is taking an input parameter of the type dynamic.
  4. In Main class, we are making object of type dynamic. This is getting resolved SampleForDynamic type at run time.
  5. Calling the dynamic type method with dynamic type argument.
  6. Printing the output.

SampleForDynamic class


public
class
SampleForDynamimc

{


public
dynamic Name { get; set; }


public
string Sex { get; set; }


public
dynamic Getinformation(dynamic param1)

{


return Name +“\t” + Sex + “\t”+ param1 ;

}

}

Operations are resolved at run time. So there is no information about operations or properties are available at run time.

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace DynamicTypeSample1

{


class
Program

{


static
void Main(string[] args)

{


dynamic obj = new
SampleForDynamimc();

obj.Name = ” Mahesh Chand”;

obj.Sex = “Male”;


dynamic message =” Welcome to Dynamimc World “;


dynamic result = obj.Getinformation(message);


Console.WriteLine(result);


Console.ReadKey(true);

}

}


public
class
SampleForDynamimc

{


public
dynamic Name { get; set; }


public
string Sex { get; set; }


public
dynamic Getinformation(dynamic param1)

{


return Name +“\t” + Sex + “\t”+ param1 ;

}

}

}

Output

Conclusion

I have talked about, dynamic type. This is a feature of c#4.0 I have shown two samples also on how to use dynamic type.

Thanks for reading my work.

Happy Coding

Silver Light 4.0 Feature #1 Printing Support

Objective

This article will introduce New Printing support in Silver Light 4.0.

Introduction

Silver Light 4.0 add support to enable business application development. Printing support allows developer to print the Silver Light application itself. Printing support helps

  1. Formatting the content of the Printing.
  2. Specifying the content of the Printing.
  3. Choosing the Printer.

How Printing support works?

While using the Printing support classes, eventually Silver Light application opens the Printing Dialog box like below.

What is new in Silver Light 4.0 for printing support?

Answer is PrintDocument class. This class provides events and function to achieve Printing in Silver Light application.

PrintDocument class

  1. This class is inside namespace System.Windows.Printing.
  2. There are three events to handles printing. They are EndPrint , StartPrint , PrintPage.
  3. There is one method called Print() to do the print operation. This method takes no input parameter.
  4. There is a property DocumentName .

How to use this class?

  1. Create instance of PrintDocument class.
  2. Specify the content to print.
  3. Write code to handle the PagePrint event.
  4. Entire SilverLight control can be printed by setting PageVisual to layout.
  5. Portion of silverlight application can be printed by setting the pagevisual tospecific control.

Diving to some example

I have a

  1. Stack Panel inside a Grid
  2. Inside Stack Panel there is one button and a text box.

<Grid x:Name=”LayoutRoot” Background=”Azure”>

<StackPanel Height=”auto” Width=”auto” x:Name=”stkPanel” Orientation=”Horizontal” >


<TextBlock x:Name=”txtBlcok” Text=”Dhananjay Kumar” Height=”40″ Width=”175″ />


<Button x:Name=”mybutton” Content=”Print” Click=”mybutton_Click” Height=”30″ Width=”50″ /></StackPanel>

</Grid>

Now I need to,

Print the text Box

  1. Create instance of PrintDocument class.
  2. Handling PrintPage event
  3. Inside the event setting PageVisual to name of the text box.
  4. Calling the Print() method in last.
  5. If you want to print the entire application, just set PageVisual to any layout like stack or Grid. In this case for stack PageVisual would be set to stkPanel.

private
void mybutton_Click(object sender, RoutedEventArgs e)

{


PrintDocument prt = new
PrintDocument();

prt.PrintPage +=(s,args) =>

{

args.PageVisual = txtBlcok;

};

prt.Print();

}

Output

On clicking of Print Button, Here you could select you Print options.

Conclusion

In this article, I gave a basic introduction of Printing Support in Silver Light 4.0. Thanks for reading.

Silver Light 4.0 feature #2: Right click Mouse events [Context Menu]

 

Objective

This article will give an introduction of mouse right click events on Silver Light controls. Silver Light 4.0 introduces context menu for all the controls.

Right Click Mouse Event is added newly added feature in Silver Light 4.0.

Design

Create a Button on silver light page.

<Grid x:Name=”LayoutRoot” Background=”Black”>

<Button x:Name=”myButton” Width=”75″ Height=”40″ Background=”Red” Content=”Click Here” />

</Grid>

Adding Right click events

Now add mouse right click events to button. Context menu or Right click event could be added to any control in Silver Light 4.0

There are two Mouse right click events.

  1. MouseRightButtonDown
  2. MouseRightButtonUp


So after adding the events

myButton.MouseRightButtonDown += new
MouseButtonEventHandler(mosuedown)

myButton .MouseRightButtonUp +=new
MouseButtonEventHandler(mouseup);

We could handle the event according to our need.

void mouseup(object sender, MouseButtonEventArgs e)

{


// Handle the event as of need

}


void mosuedown(object sender, MouseButtonEventArgs e)

{


// Handle the event as of need

}

While running above code, we will get below output. Once right click on button, default context of silver light will come. See the images below.

On clicking on Silver Light, Silver Light configuration window will get open. See the image below.

Disabling default context of Silver Light

We need to tell Silver Light that context has been handled by making handled as True.

void mosuedown(object sender, MouseButtonEventArgs e)

{

e.Handled = true;

}

After enabling handled true the default context of Silver Light will be disabled. Now if you run the application, you won’t get default silver light context on right click.

Now, adding a message box to handle the events.

Complete code is as below

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace TestingRightClickEvents

{


public
partial
class
MainPage : UserControl

{


public MainPage()

{

InitializeComponent();

myButton.MouseRightButtonDown += new
MouseButtonEventHandler(mosuedown);

myButton .MouseRightButtonUp +=new
MouseButtonEventHandler(mouseup);

}


void mouseup(object sender, MouseButtonEventArgs e)

{


MessageBox.Show(“Right Click of Button is Up “);

}


void mosuedown(object sender, MouseButtonEventArgs e)

{

e.Handled = true;

}

}

}

Output

Conclusion

In this article, I introduced Context menu or Mouse Right click event handling in Silver Light 4.0 Thanks for reading.

Step by Step Installing Silver Light 4.0 beta

  

Objective 

In this tutorial , I will give step by step pictorial representation of installing Silver Light 4.0 Beta.

Minimum Requierment 

 Silver Light 4.0 beta can only be installed in Visual Studio 2010.

Step 1

Install Silver Light 4.0 developer run time

Link to dwonload Developer runtime  is below,

http://www.microsoft.com/downloads/details.aspx?FamilyID=9fa8afe9-cad6-4090-a7f6-7d9cdc560e2d&displaylang=en

 After downloading , just run the application. Follow the steps as given below.

 Now Silver Light 4.0 run time has been installed on your system.

Step 2

Install Silver Light 4.0 SDK

Download Silver Light 4.0 SDK from below link and install it,

http://go.microsoft.com/fwlink/?LinkID=169408

Upto this Step Silver Light 4.0 SDK has been installed on your system.

Step 3

Install Blend for Silver Light 4.0

Download  from below link

http://www.microsoft.com/downloads/details.aspx?FamilyID=6806e466-dd25-482b-a9b3-3f93d2599699&displaylang=en

and install Belnd 4.0 Beta for Silver Light 4.0 .

Step 4

Install Silver Light 4.00 toolkit

To install Silver Light toolkit download from below link

http://silverlight.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=36060

Now Silver Light 4.0 toolkits has been installed on your system. 

After installing you should able to Silver Light Configuration wizard in Start menu. Like below.

And in visual studio , you should able to get all Silver Light 4.0 project templates.

Conclusion

In this tutorial ,  I shown how  to install Silver Light 4.0 Beta on Visual Studio 2010. Thanks for reading.