#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 ConstructorExpression : Expression
    {
        private readonly VariableExpression _className;
        private readonly Expression[] _parameters;

        public ConstructorExpression(VariableExpression className, Expression[] parameters)
        {
            _className = className;
            _parameters = parameters;
        }

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

            if (className == null)
                throw new TypeInitializationException(_className.Variable, null);

            return Expression.Value(className.Type.GetConstructors());
        }

        public override string ToString()
        {
            string[] parameters = Array.ConvertAll<Expression, string>(_parameters, 
                delegate(Expression expr) { return expr.ToString(); });
            return "(new " + _className + "(" + String.Join(",", parameters) + "))";
        }
    }
}

