Member Exists – Dynamic C# 4.0

I have been using the dynamic keyword a little bit in C# and I have recently run into a small problem and I have been trying to figure out the most elegant way to resolve it. The problem is, when you try to invoke a member that does not exist on a dynamic object it throws an exception, there is no easy way to detect if the member exists built-in to accommodate this.

For example what I would really like is an ‘exists’ keyword, something like:

dynamic instance = new { Foo = "Hello World!" };
if (exists(instance.Foo))
{
Console.WriteLine(instance.Foo);
}

But there appears to be no magic ‘exists’. Turns out the only way to do this is with reflection. So I created some helper extension methods to enable the following example:

object instance = new { Foo = "Hello World!" };

if (instance.Reflection().Exists().Foo)
{
    string value = instance.Reflection().Call().Foo;
    Console.WriteLine(value);
}

Interestingly enough the Foo in this case is a call to a DynamicObject I created that can intercept calls to members and returns a bool if they exist, never calling them. The Reflection().Call() simply casts any object into a dynamic object for dynamic calling.

I also found out that you can’t use the dynamic Type as the first parameter in an extension method. Dynamic objects and extension methods don’t really play together very nicely.

Download the full example here:

https://onedrive.live.com/redir?resid=DFCD2D88D3FE101C%21341

Creating an internal DSL with MetaSharp

Inside of MetaSharp is a CLS compliant language using the same system and patterns that you would use to create an external DSL. One of the parts of creating your own DSL in MetaSharp is declaring Node objects to represent your parsed graph. MetaSharp will convert your parsed MGrammar graph into your strongly Typed AST (Nodes) which you can then use to transform however you wish.

Declaring your AST nodes requires a certain design pattern and after some prompting from Attila the Hun I have created a DSL specifically for creating nodes. Here it is before the DSL:

//-----------------------------------------------------------------------
// <copyright company="MetaSharp">
//     Copyright (c) MetaSharp. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace MetaSharp.Lang.Ast.Standard
{
    using System.Collections.Generic;
    using MetaSharp.Lang.Ast.Common;

    /// <summary>
    /// A foreach statement node.
    /// </summary>
    public class ForEachStatement : Statement
    {
        /// <summary>
        /// The VariableType NodeProperty.
        /// </summary>
        public static readonly NodeProperty VariableTypeProperty = 
            NodeProperty.Register<ForEachStatement>(n => n.VariableType);

        /// <summary>
        /// The VariableName NodeProperty.
        /// </summary>
        public static readonly NodeProperty VariableNameProperty = 
            NodeProperty.Register<ForEachStatement>(n => n.VariableName);

        /// <summary>
        /// The Expression NodeProperty.
        /// </summary>
        public static readonly NodeProperty ExpressionProperty = 
            NodeProperty.Register<ForEachStatement>(n => n.Expression);

        /// <summary>
        /// The Statements NodeProperty.
        /// </summary>
        public static readonly NodeProperty StatementsProperty = 
            NodeProperty.Register<ForEachStatement>(n => n.Statements);

        /// <summary>
        /// Gets the variable type.
        /// </summary>
        public TypeReference VariableType
        {
            get { return (TypeReference)this.GetValue(VariableTypeProperty); }
            set { this.SetValue(ForEachStatement.VariableTypeProperty, value); }
        }

        /// <summary>
        /// Gets the variable name.
        /// </summary>
        public string VariableName
        {
            get { return (string)this.GetValue(VariableNameProperty); }
            set { this.SetValue(ForEachStatement.VariableNameProperty, value); }
        }

        /// <summary>
        /// Gets the enumeration expression.
        /// </summary>
        public Expression Expression
        {
            get { return (Expression)this.GetValue(ExpressionProperty); }
            set { this.SetValue(ForEachStatement.ExpressionProperty, value); }
        }

        /// <summary>
        /// Gets the statements.
        /// </summary>
        public IEnumerable<Statement> Statements
        {
            get { return (IEnumerable<Statement>)this.GetValue(StatementsProperty); }
            set { this.SetValue(ForEachStatement.StatementsProperty, value); }
        }
    }
}

And here it is as a DSL:

//-----------------------------------------------------------------------
// <copyright company="MetaSharp">
//     Copyright (c) MetaSharp. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace MetaSharp.Lang.Ast.Standard:

    import System.Collections.Generic;
    import MetaSharp.Lang.Ast.Common;
    import MetaSharp.Transformation;

    /// <summary>
    /// A foreach statement node.
    /// </summary>
    node ForEachStatement as Statement:
    
        /// <summary>
        /// The VariableType NodeProperty.
        /// </summary>
        property VariableType as TypeReference;

        /// <summary>
        /// The VariableName NodeProperty.
        /// </summary>
        property VariableName as string;

        /// <summary>
        /// The Expression NodeProperty.
        /// </summary>
        property Expression as Expression;

        /// <summary>
        /// The Statements NodeProperty.
        /// </summary>
        property Statements as IEnumerable<Statement>;

    end
end

It went from 76 lines to 38, so that’s a win in my book. Plus most of the lines that are there are much shorter. The only downside is the lack of intellisense and syntax hilighting but I have reason to believe that that is a solvable problem in general if you’re using MGrammar as your parser, since it’s already possible in Intellipad.

To use this DSL all you have to do is import MetaSharp.Lang.targets into your .csproj file.

<Import Project="$(NodeBuilderBinPath)\MetaSharp.Lang.targets" />

Then in your project you simply add your items to the project as Nodes. Like so:

image

This will generate a file for you at compile time in your projects language (i.e. this should work in VB as well) and that file will get compiled along with the assembly. Next I want to build a Pipeline DSL, then the process of building your own DSL will all be done in DSLs as well!