Click here to Skip to main content
15,886,724 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 43.4K   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.Collections;
using System.Windows.Markup;

namespace MarkupScript
{
    /// <summary>
    /// Base class for all unary expressions.
    /// Unary expressions evaluate their value property.
    /// </summary>
    public abstract class UnaryExpression : Statement, IExpression
    {
        /// <summary>
        /// The expression to evaluate.
        /// </summary>
        [ConstructorArgument("value")]
        public IExpression Value { get; set; }

        /// <summary>
        /// The evaluator for the specific expression.
        /// </summary>
        private Func<object, object> evaluator;

        protected UnaryExpression(Func<object, object> evaluator)
        {
            this.evaluator = evaluator;
        }

        public override object OnEvaluate()
        {
            return evaluator(Value.Evaluate());
        }
    }

    public class Not : UnaryExpression, IStatement
    {
        public Not() : base(a => !ToBoolean(a)) { }
    }

    public class Negate : UnaryExpression
    {
        public Negate() : base(a => -ToDouble(a)) { }
    }

    public class Count : UnaryExpression
    {
        public Count() : base(a => GetCount(a)) { }

        private static object GetCount(object value)
        {
            IList list = value as IList;
            if (list != null)
            {
                return list.Count;
            }

            IEnumerable enumerable = value as IEnumerable;
            if (enumerable != null)
            {
                return enumerable.Cast<object>().Count();
            }

            return 0;
        }
    }

    public class Random : Statement, IStatement, IExpression
    {
        private static System.Random random = new System.Random();

        public double MinValue { get; set; }
        public double MaxValue { get; set; }

        public Random()
            : this(1.0)
        {
        }

        public Random(double maxValue)
        {
            this.MaxValue = maxValue;
        }

        public override object OnEvaluate()
        {
            return random.Next((int)MinValue, (int)MaxValue);
        }
    }
}

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