Click here to Skip to main content
15,878,809 members
Articles / Desktop Programming / XAML

And Now for XAML Completely Different

Rate me:
Please Sign up or sign in to vote.
4.92/5 (46 votes)
1 Apr 2010CPOL6 min read 43K   397   59  
Using Markup Extensions to build individual markup based declarative systems with XAML
//
// MarkupScript                                                  
// Author: Tom Englert
// Contact: mail@tom-englert.de
// Copyright (C) 2010 tom-englert.de
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Collections;

namespace MarkupScript
{
    /// <summary>
    /// The Get expression. Returns the value of a variable or array member.
    /// </summary>
    public class Get : Statement, IExpression
    {
        /// <summary>
        /// The name of the variable to access.
        /// </summary>
        public IExpression Variable { get; set; }
        /// <summary>
        /// Optional index of the array element to return. Leave this value empty to access the variable itselve.
        /// </summary>
        public IExpression Index { get; set; }
        /// <summary>
        /// Optional name of the array element to return. Leave this value empty to access the variable itselve.
        /// </summary>
        public IExpression Member { get; set; }

        /// <summary>
        /// Flag to indicate if the function should fail if the variable could not be found. 
        /// If Optional is true no error will be generated when the variable does not exist.
        /// </summary>
        public IExpression Optional { get; set; }

        public Get()
        {
        }

        public Get(IExpression variable)
        {
            this.Variable = variable;
        }

        public override object OnEvaluate()
        {
            string variableName = ToString(TryEvaluate(Variable));
            IVariable variable = ToBoolean(TryEvaluate(this.Optional)) ? TryFindVariable(variableName) : FindVariable(variableName);

            if (variable == null)
                return null;

            if (Index != null)
            {
                return GetIndex(variable.Value, (int)ToDouble(TryEvaluate(Index)));
            }
            else if (Member != null)
            {
                return GetMember(variable.Value, ToString(TryEvaluate(Member)));
            }
            else
            {
                return variable.Value;
            }
        }

        private static object GetIndex(object variable, int index)
        {
            try
            {
                IList list = variable as IList;
                if (list != null)
                {
                    return new ConstantExpression(list[index]).Evaluate();
                }

                IEnumerable enumerable = variable as IEnumerable;
                if (enumerable != null)
                {
                    return new ConstantExpression(enumerable.Cast<object>().ElementAt(index)).Evaluate();
                }
            }
            catch (IndexOutOfRangeException)
            {
            }

            return null;
        }

        private static object GetMember(object variable, string name)
        {
            IEnumerable enumerable = variable as IEnumerable;
            if (enumerable != null)
            {
                IVariable value = enumerable.OfType<IVariable>().FirstOrDefault(e => e.Name == name);
                if (value != null)
                {
                    return value.Value;
                }
            }

            return null;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions