#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace System.Text.Template
{
    public class TypeCastExpression : Expression
    {
        private readonly VariableExpression _typeExpression;
        private readonly Expression _targetExpression;

        public TypeCastExpression(VariableExpression typeExpression, Expression targetExpression)
        {
            _typeExpression = typeExpression;
            _targetExpression = targetExpression;
        }

        public override ValueExpression Evaluate(ITemplateContext context)
        {
            ClassName className = _typeExpression.Evaluate(context).Value as ClassName;

            if (className == null)
                throw new ArgumentException("Type cast requires a type name");

            return Expression.Value(Convert.ChangeType(_targetExpression.Evaluate(context).Value, className.Type), className.Type);
        }
    }
}
