示例#1
0
 internal override Result Eval(Evaluator evaluater, Result[] argArray)
 {
     return(new Result(typeof(int), ~((uint)ConvertHelper.ChangeType(argArray[0].Value, typeof(uint)))));
 }
示例#2
0
        protected override void ProcessArg(Evaluator evaluater, Result[] argArray)
        {
            base.ProcessArg(evaluater, argArray);
            Type type0 = argArray[0].Type;
            Type type1 = argArray[1].Type;
            int  result;

            if (type0 == null && type1 == null)
            {
                return;
            }

            if (type0 == null)
            {
                if (type1.IsValueType)
                {
                    throw new InvalidCastException(string.Format("from null to {0}", type1.Name));
                }

                if (type1 == typeof(string))
                {
                    argArray[0].Type = typeof(string);
                    return;
                }
                else
                {
                    throw new InvalidCastException(string.Format("from null to {0}", type1.Name));
                }
            }

            if (type1 == null)
            {
                if (type0.IsValueType)
                {
                    throw new InvalidCastException(string.Format("from null to {0}", type0.Name));
                }

                if (type0 == typeof(string))
                {
                    argArray[1].Type = typeof(string);
                    return;
                }
                else
                {
                    throw new InvalidCastException(string.Format("from null to {0}", type0.Name));
                }
            }

            if (BinaryHelper.ComparePrimitiveType(type0, type1, out result))
            {
                if (result > 0)
                {
                    argArray[1].Value = ConvertHelper.ChangeType(argArray[1].Value, type0);
                }
                else if (result < 0)
                {
                    argArray[0].Value = ConvertHelper.ChangeType(argArray[0].Value, type1);
                }
            }
            else
            {
                throw new InvalidCastException(string.Format("from {1} to {0}", type0.Name, type1.Name));
            }
        }
示例#3
0
        internal override Result Eval(Evaluator evaluater, Result[] argArray)
        {
            if (argArray.Length < 2)
            {
                throw new ArgumentException();
            }

            object target = argArray[0].Value;

            if (target == null)
            {
                throw new ArgumentException();
            }
            int length = argArray.Length - 1;

            Type         type         = null;
            BindingFlags bindingFlags = BindingFlags.Default;

            if (target is Type)
            {
                type         = (Type)target;
                target       = null;
                bindingFlags = BindingFlags.Static;
            }
            else
            {
                type         = target.GetType();
                bindingFlags = BindingFlags.Instance;
            }
            bindingFlags = BindingFlags.Public;

            Type[]   argTypeArray = new Type[length];
            object[] argValueAry  = new object[length];
            for (int i = 0; i < length; i++)
            {
                argTypeArray[i] = argArray[i + 1].Type;
                argValueAry[i]  = argArray[i + 1].Value;
            }

            Binder       defaultBinder = Type.DefaultBinder;
            PropertyInfo pi            = type.GetProperty("Item", bindingFlags, defaultBinder, null, argTypeArray, null);

            if (pi == null)
            {
                pi = type.GetProperty("Items", bindingFlags, defaultBinder, null, argTypeArray, null);
            }
            if (pi != null)
            {
                return(new Result(pi.PropertyType, pi.GetValue(target, argValueAry)));
            }

            if (target == null)
            {
                throw new ArgumentException();
            }

            object ret;

            if (target is string)
            {
                string s     = (string)target;
                int    index = (int)ConvertHelper.ChangeType(argArray[1].Value, typeof(int));
                ret = s[index];
                return(new Result(typeof(char), ret));
            }

            if (target is Array)
            {
                Array array = (Array)target;
                if (array.Rank != (argArray.Length - 1))
                {
                    throw new ArgumentException();
                }

                int[] indices = new int[length];
                for (int i = 0; i < length; i++)
                {
                    indices[i] = (int)ConvertHelper.ChangeType(argArray[i + 1].Value, typeof(int));
                }

                ret = array.GetValue(indices);
                if (ret != null)
                {
                    return(new Result(ret.GetType(), ret));
                }
                return(new Result(null, null));
            }

            if (target is IList)
            {
                IList list = (IList)target;
                if (argArray.Length != 2)
                {
                    throw new ArgumentException();
                }
                int index = (int)ConvertHelper.ChangeType(argArray[1].Value, typeof(int));
                ret = list[index];
                if (ret != null)
                {
                    return(new Result(ret.GetType(), ret));
                }
                return(new Result(null, null));
            }

            throw new ArgumentException();
        }