Get Dynamic with C# 4.0

C# 4.0 is already around the corner and I haven’t even had time to wrap my head around LINQ, that stuff C# 3.0 was all about. (I really need to read a book or two about that, maybe I’ll include it as a new year resolution.)

Back to dynamic, today I finally installed VS 2010 Beta 2 with all the 4.0 goodies (C# and .NET Framework) and the first thing I had to try was, of course, the new dynamic keyword.

To do this I decided to revisit my Command Line Parser and add some dynamism to it. The goal would be to simplify it’s typical usage illustrated in the following code:

// Mandatory options
string dataFile = (string)cmdLine.Options["DataFile"].Value;
string schemaFile = (string)cmdLine.Options["SchemaFile"].Value;

// Flag and Switch options always have a value
bool verbose = (bool)cmdLine.Options["Verbose"].Value;
bool test = (bool)cmdLine.Options["UseTransaction"].Value;

// Check if the value for a non mandatory option is available
string connString;
if (cmdLine.Options["ConnectionString"].Available)
{
    connString = (string)cmdLine.Options["ConnectionString"].Value;
}

The above code would be a lot nicer if it weren’t for those casts and string literal references, so to remove them I implemented a new class inheriting from DynamicObject and added a method to the already existing OptionCollection class to return it wrapped inside the new dynamic object. Here’s the code:

public sealed class OptionCollection : Collection
{
    // ...

    public dynamic AsDynamic()
    {
        return new DynamicOptions(this);
    }
}

internal sealed class DynamicOptions : DynamicObject
{
    public DynamicOptions(OptionCollection options)
    {
        this.Options = options;
    }

    private OptionCollection Options { get; set; }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (this.Options.Contains(binder.Name))
        {
            result = this.Options[binder.Name];

            return true;
        }

        return base.TryGetMember(binder, out result);
    }
}

With this I can now access and use the command line options like this:

// Get the options as dynamic
dynamic options = cmdLine.Options.AsDynamic();

// Mandatory options
string dataFile = options.DataFile.Value;
string schemaFile = options.SchemaFile.Value;

// Flag and Switch options always have a value
bool verbose = options.Verbose.Value;
bool test = options.UseTransaction.Value;

// Check if the value for a non mandatory option is available
string connString;
if (options.ConnectionString.Available)
{
    connString = options.ConnectionString.Value;
}

Pretty sweet, no?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s