Пример #1
0
        public static bool Equals(Expose left, Expose right)
        {
            object lv = ReferenceEquals(left, null) ? null : left.Value;
            object rv = ReferenceEquals(right, null) ? null : right.Value;

            return(ReferenceEquals(lv, null) ? ReferenceEquals(rv, null) : Expose.Equals(left, rv));
        }
Пример #2
0
        static bool TryInvoke(string methodName, object instance, IEnumerable <MethodInfo> methodInfos, Type[] genericTypes, object[] args, out Expose result)
        {
            bool isGeneric = genericTypes != null && 0 < genericTypes.Length;

            foreach (MethodInfo methodInfo in methodInfos)
            {
                MethodInfo mi = methodInfo;
                if (mi.Name != methodName || isGeneric && !mi.IsGenericMethod)
                {
                    continue;
                }

                try
                {
                    if (isGeneric)
                    {
                        mi = mi.GetGenericMethodDefinition().MakeGenericMethod(genericTypes);
                    }

                    if (IsInvokable(mi, args))
                    {
                        result = Expose.FromObject(mi.Invoke(instance, args));
                        return(true);
                    }
                }
                catch
                {
                }
            }
            result = null;
            return(false);
        }
Пример #3
0
        public static bool Equals(Expose left, object right)
        {
            try
            {
                return((bool)left.Call("op_Equality", right).Value);
            }
            catch
            { }

            try
            {
                return(ToDecimal(left) == ToDecimal(right));
            }
            catch
            { }

            try
            {
                return((bool)left.Call("Equals", right).Value);
            }
            catch
            { }

            object lv = ReferenceEquals(left, null) ? null : left.Value;
            object rv = right;

            return(ReferenceEquals(lv, null) ? ReferenceEquals(rv, null) : lv.Equals(rv));
        }
Пример #4
0
        static Expose Addition(Expose left, object right)
        {
            try
            {
                if (IsNumber(left) && IsNumber(right))
                {
                    var d = ToDecimal(left) + ToDecimal(right);
                    return(Expose.FromObject(ToDecimal(left) + ToDecimal(right)));
                }
                return(Expose.FromType(left.Type).Call("op_Addition", left.Value, right));
            }
            catch
            {
            }

            object lv = ReferenceEquals(left, null) ? null : left.Value;

            if (lv is string || right is string)
            {
                string ls = ReferenceEquals(lv, null) ? "" : lv.ToString();
                string rs = ReferenceEquals(right, null) ? "" : right.ToString();
                return(Expose.FromObject(ls + rs));
            }

            try
            {
                return(Expose.FromObject(ToDecimal(left) + ToDecimal(right)));
            }
            catch
            {
            }
            return(null);
        }
Пример #5
0
        public Expose Get(string memberName)
        {
            var pi = Type.GetProperty(memberName, Flag | BindingFlags.GetProperty);

            if (pi != null)
            {
                return(Expose.FromObject(pi.GetValue(Value)));
            }
            var fi = Type.GetField(memberName, Flag | BindingFlags.GetField);

            if (fi != null)
            {
                return(Expose.FromObject(fi.GetValue(Value)));
            }

            // Get with indexer
            try
            {
                return(Get(new[] { memberName }));
            }
            catch
            {
                throw new MissingMemberException(Type.Name, memberName);
            }
        }
Пример #6
0
        public Expose Get(object[] index)
        {
            var pi = Type.GetProperty(Type.GetCustomAttribute <DefaultMemberAttribute>().MemberName, index.Select(x => x.GetType()).ToArray());

            if (pi != null)
            {
                return(Expose.FromObject(pi.GetValue(Value, index)));
            }

            throw new MissingMemberException(Type.Name, "Indexer");
        }
Пример #7
0
 public IEnumerator GetEnumerator()
 {
     if (Value is IEnumerable)
     {
         foreach (object o in Value as IEnumerable)
         {
             yield return(Expose.FromObject(o));
         }
     }
     yield break;
 }
Пример #8
0
        static bool TryInvoke(string methodName, object instance, IEnumerable <MethodInfo> methodInfos, object[] args, out Expose result)
        {
            var methodInfo = methodInfos.FirstOrDefault(x => x.Name == methodName && IsInvokable(x, args));

            if (methodInfo != null)
            {
                result = Expose.FromObject(methodInfo.Invoke(instance, args));
                return(true);
            }
            result = null;
            return(false);
        }
Пример #9
0
        public Expose Call(string methodName, params object[] args)
        {
            args = args.Select(x => x is Expose ? ((Expose)x).Value : x).ToArray();

            // Find method
            if (TryInvoke(methodName, Value, Type.GetMethods(Flag | BindingFlags.InvokeMethod), args, out Expose result))
            {
                return(result);
            }

            // Find event
            var fi = Type.GetField(methodName, Flag | BindingFlags.SetField);

            if (fi != null)
            {
                var del = fi.GetValue(Value) as MulticastDelegate;
                if (del != null && IsInvokable(del.Method, args))
                {
                    object lastResult = null;
                    foreach (var h in del.GetInvocationList())
                    {
                        lastResult = h.Method.Invoke(h.Target, args);
                    }
                    return(Expose.FromObject(lastResult));
                }
            }

            // Find operator method
            args = new[] { Value }.Concat(args).ToArray();
            if (TryInvoke(methodName, null, Type.GetMethods(Flag | BindingFlags.InvokeMethod), args, out result))
            {
                return(result);
            }

            // Find extension method
            if (Value != null &&
                TryInvoke(methodName, null, s_extensionMethods.Where(x => Is(Type, x.Key)).SelectMany(x => x.Value), null, args, out result))
            {
                if (
                    TryInvoke(methodName, null, s_extensionMethods.Where(x => Is(Type, x.Key)).SelectMany(x => x.Value), null, args, out result) ||
                    TryInvoke(methodName, null, s_extensionMethods.Where(x => Is(Type, x.Key)).SelectMany(x => x.Value), new[] { Type }, args, out result)
                    )
                {
                    return(result);
                }
            }

            throw new MissingMemberException(Type.Name, methodName);
        }
Пример #10
0
        static Expose Subtraction(Expose left, object right)
        {
            try
            {
                return(Expose.FromType(left.Type).Call("op_Subtraction", left.Value, right));
            }
            catch
            { }

            try
            {
                return(Expose.FromObject(ToDecimal(left) - ToDecimal(right)));
            }
            catch
            { }

            return(null);
        }
Пример #11
0
        static bool LessThan(Expose left, object right)
        {
            try
            {
                return((bool)Expose.FromType(left.Type).Call("op_LessThan", left.Value, right).Value);
            }
            catch
            { }

            try
            {
                return(ToDecimal(left) < ToDecimal(right));
            }
            catch
            {
                return(Expose.Compare(left, right) < 0);
            }
        }
Пример #12
0
 public static bool operator !=(object left, Expose right)
 {
     return(!Expose.Equals(right, left));
 }
Пример #13
0
 public static Expose operator -(object left, Expose right)
 {
     return(Subtraction(Expose.FromObject(left), right.Value));
 }
Пример #14
0
 public static Expose operator -(Expose left, Expose right)
 {
     return(Expose.Subtraction(left, right.Value));
 }
Пример #15
0
 public static Expose operator +(object left, Expose right)
 {
     return(Addition(Expose.FromObject(left), right.Value));
 }
Пример #16
0
 public static Expose operator +(Expose left, Expose right)
 {
     return(Expose.Addition(left, right.Value));
 }
Пример #17
0
        static Expose DoOperation(Expose left, object right, Operation op)
        {
            try
            {
                return(Expose.FromType(left.Type).Call(op.ToString(), left.Value, right));
            }
            catch
            { }

            try
            {
                switch (op)
                {
                case Operation.op_Addition:
                    return(Expose.FromObject(ToDecimal(left) + ToDecimal(right)));

                case Operation.op_Subtraction:
                    return(Expose.FromObject(ToDecimal(left) - ToDecimal(right)));

                case Operation.op_Equality:
                    return(Expose.FromObject(ToDecimal(left) == ToDecimal(right)));

                case Operation.op_Inequality:
                    return(Expose.FromObject(ToDecimal(left) != ToDecimal(right)));

                case Operation.op_GreaterThan:
                    return(Expose.FromObject(ToDecimal(left) > ToDecimal(right)));

                case Operation.op_LessThan:
                    return(Expose.FromObject(ToDecimal(left) < ToDecimal(right)));

                case Operation.op_GreaterThanOrEqual:
                    return(Expose.FromObject(ToDecimal(left) <= ToDecimal(right)));

                case Operation.op_LessThanOrEqual:
                    return(Expose.FromObject(ToDecimal(left) >= ToDecimal(right)));

                case Operation.op_BitwiseAnd:
                    return(Expose.FromObject(ToInt(left) & ToInt(right)));

                case Operation.op_BitwiseOr:
                    return(Expose.FromObject(ToInt(left) | ToInt(right)));

                case Operation.op_Division:
                    return(Expose.FromObject(ToDecimal(left) / ToDecimal(right)));

                case Operation.op_Modulus:
                    return(Expose.FromObject(ToDecimal(left) % ToDecimal(right)));

                case Operation.op_Multiply:
                    return(Expose.FromObject(ToDecimal(left) * ToDecimal(right)));

                case Operation.op_LeftShift:
                    return(Expose.FromObject(ToInt(left) << ToInt(right)));

                case Operation.op_RightShift:
                    return(Expose.FromObject(ToInt(left) >> ToInt(right)));

                case Operation.op_ExclusiveOr:
                    return(Expose.FromObject(ToInt(left) ^ ToInt(right)));

                case Operation.op_UnaryNegation:
                    break;

                case Operation.op_UnaryPlus:
                    break;

                case Operation.op_LogicalNot:
                    break;

                case Operation.op_OnesComplement:
                    break;

                case Operation.op_False:
                    break;

                case Operation.op_True:
                    break;

                case Operation.op_Increment:
                    break;

                case Operation.op_Decrement:
                    break;
                }
            }
            catch
            { }

            return(null);
        }
Пример #18
0
 public static bool operator !=(Expose left, object right)
 {
     return(!Expose.Equals(left, right));
 }
Пример #19
0
 public static bool operator ==(Expose left, Expose right)
 {
     return(Expose.Equals(left, right));
 }
Пример #20
0
 public static int Compare(Expose left, object right)
 {
     return(Compare_Impl(ReferenceEquals(left, null) ? null : left.Value, right));
 }
Пример #21
0
 public static int Compare(Expose left, Expose right)
 {
     return(Compare_Impl(
                ReferenceEquals(left, null) ? null : left.Value,
                ReferenceEquals(right, null) ? null : right.Value));
 }
Пример #22
0
 public bool Equals(Expose other)
 {
     return(Equals(this, other));
 }