- Posted by Shay Friedman on December 19, 2008
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:
- myst variable is created.
- Revealer.RevealSecrets gets the myst variable as dynamic.
- 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.
- 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.
- Posted by Shay Friedman on December 14, 2008
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
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
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
Installation
You can get the installer from: http://www.codeplex.com/IronScheme/Release/ProjectReleases.aspx?ReleaseId=14059
Resources
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 codeplex homepage: http://www.codeplex.com/dlr/Release/ProjectReleases.aspx?ReleaseId=20378
Resources
Hope it helps,
Shay.
- Posted by Shay Friedman on December 13, 2008
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
2. Select ir.exe from the [IronRuby code directory]\trunk\build\debug (or release, depends on how you've compiled the code)
3. Right click ir.exe in Solution Explorer and select Properties
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".
That's it! you can debug your IronRuby code file now!
This post was greatly inspired by the IronPython solution that was posted by Harry Pierson on his blog.
Enjoy,
Shay.