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

namespace FormulaEditor
{
    /// <summary>
    /// Operation of property
    /// </summary>
    public class PropertyOperation : IObjectOperation
    {
        private string propertyName;
        private object obj;
        private PropertyInfo propertyInfo;
        private Type objectType;

        public PropertyOperation(object obj, string propertyName)
        {
            this.obj = obj;
            this.propertyName = propertyName;
            objectType = obj.GetType();
            propertyInfo = objectType.GetProperty(propertyName);
            if (propertyInfo == null)
            {
                throw new Exception();
            }
        }
        #region IObjectOperation Members

        public int Arity
        {
            get
            {
                return 1;
            }
        }

        public object this[object[] x]
        {
            get
            {
                object obj = x[0];
                return propertyInfo.GetValue(x[0], null);
            }
        }

        public object ReturnType
        {
            get
            {
                return new object[] { obj, propertyName };
            }
        }

        public bool IsPowered
        {
            get
            {
                return false;
            }
        }

        #endregion
    }
}
