DCSIMG
December 2008 - Posts - IronShay

December 2008 - Posts

The dynamic Keyword Part 1 - Introduction

Firstly, in order to use the dynamic keyword, you'll have to download the VS2010 VPC image and work there. There is no way currently, to download and install Visual Studio 2010 on your local computer.

Now we're ready to explore the future!

So what is the dynamic keyword?

The dynamic keyword is a new type of variable that will be added to the syntax of C# 4.0. Behind the scenes, the dynamic keyword is declared as an object with an attribute that indicates that this is a "special" object that should be treated dynamically.

The way you declare it is just like any other variable type:

dynamic myDynamicVariable = something;

What is it good for?

This dynamic mechanism is a wonderful thing for a bunch of different things that now we have trouble with. Things like - reflection in different environments, calling dynamic languages, dynamic fluent interfaces and more. All of these will become tremendously easier, using the dynamic keyword.

Example - Reflection using dynamic

Let's say I have a type that I define on one class library, I'll call it TheMysteriousType. This is its code:

namespace Mysterious
{
public class TheMysteriousType
{
public string RevealYourSecrets()
{
return "Never!";
}
}
}

Now, I'll create another class library with a method that receives a dynamic variable and executes the RevealYourSecrets method:

namespace Revealer
{
public class Revealer
{
public static void RevealSecrets(dynamic obj)
{
Console.WriteLine("Reveal you secrets, object! {0}", obj.RevealYourSecrets());
}
}
}

And last thing - I'll create a console application that has the above 2 class libraries references. It will contain the following code:

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Mysterious.TheMysteriousType myst = new Mysterious.TheMysteriousType();
Revealer.Revealer.RevealSecrets(myst);
}
}
}

The flow of the application goes like this:

  1. myst variable is created.
  2. Revealer.RevealSecrets gets the myst variable as dynamic.
  3. Inside Revealer.RevealSecrets: obj.RevelarYourSecrets() will generate a dynamic invocation of the RevealYourSecrets method. Behind the curtains, this will end up running the traditional reflection code for us.
  4. The application ends up writing to the screen: "Reveal your secrets, object! Never!".

This is a sample reflection scenario - the Revealer class does not know the object it receives. In order to invoke the RevealYourSecrets method prior to C# 4, we had to use some complicated reflection code. From C# 4, we'll have the dynamic keyword on our side that will do that for us.

This is great because it saves us time and make our code more readable, eventually making it easier to maintain.

Stay tuned to the next parts!
Happy Holidays!
Shay.

kick it on DotNetKicks.com
Posted by shayf | 3 comment(s)

My Presentation at Developer Academy 3: Dynamic Languages and The .Net Framework

Thanks everybody for coming to my session. It was a great great fun, even though I didn't get to show you the last demos. I'll make it up to you by a series of posts about the dynamic keyword, so stay tuned!

My presentation can be fetched from here. The demos are available from here - pay attention to the instructions on the readme file first.
If you've missed the session, you can watch the recording - download or watch online.

[If the links are broken for some reason, you can go to the conference sessions page and get the files from there (DEV312)]

In order to start with dynamic languages and get impressed, take a look at my previous post - "Getting Started with Dynamic Languages".

If you have any questions, you can use the contact form and get in touch with me.

Thanks again for coming,
Shay.

Posted by shayf | with no comments
תגים:,

Getting Started With Dynamic Languages

I've grouped together some resources and blogs for all of you out there who are willing to start working with dynamic languages that are built on top of the DLR. Enjoy!

IronRuby IronRuby

Installation

Currently you'll have to get the code from the IronRuby SVN repository (svn://rubyforge.org/var/svn/ironruby  OR HTTP://ironruby.rubyforge.org/svn/trunk/) and build the project yourself.
Justin Etheredge has posted a step-by-step walk-through on his blog.

Resources

Recommended Blogs

IronPython IronPython

Installation

You can get the installer from the IronPython codeplex homepage: http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=8365

Resources

Recommended Blogs

IronScheme  IronScheme

 Installation

You can get the installer from: http://www.codeplex.com/IronScheme/Release/ProjectReleases.aspx?ReleaseId=14059

Resources

The Dynamic Language Runtime DLR - The Dynamic Language Runtime

There is no need to download the DLR code separately because it already comes with the installation of the other languages. If you want to write your own language on top of the DLR, this is the place to start for you.

Installation

You can get the binaries and the code from the codeplaex homepage: http://www.codeplex.com/dlr/Release/ProjectReleases.aspx?ReleaseId=20378

Resources

Hope it helps,
Shay.

kick it on DotNetKicks.com

How to Debug and Run IronRuby Code From Visual Studio

This isn't a trivial thing to do until the IronRuby Visual Studio Integration component is out. So here are the steps in order to achieve that:

1. In Visual Studio, click on File-> Open -> Project/Solution

How to Debug and Run IronRuby Code From Visual Studio

2. Select ir.exe from the [IronRuby code directory]\trunk\build\debug (or release, depends on how you've compiled the code)

How to Debug and Run IronRuby Code From Visual Studio

3. Right click ir.exe in Solution Explorer and select Properties

How to Debug and Run IronRuby Code From Visual Studio

4. In Command Arguments, with "-D [path to code file]" where [path to code file] will be the full path to your ruby file.
For example, -D "c:\dev\IronRubyTest\test.rb".

How to Debug and Run IronRuby Code From Visual Studio

That's it! you can debug your IronRuby code file now!

image

This post was greatly inspired by the IronPython solution that was posted by Harry Pierson on his blog.

Enjoy,
Shay.

IronPython 2.0 is Up For Grabs!

It's holiday times and IronPython joins the celebrations! Microsoft has just announced the final release of IronPython - the Python  IronPython 2.0 is up for grabs!implementation on top of the DLR. This version is named IronPython 2.0.

Go and get it: http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=8365

Happy Holidays!
Shay.

Posted by shayf | with no comments