예제 #1
0
        public override bool Visit(object obj, VisitorContext context)
        {
            if (obj == null)
            {
                return(false);
            }

            var type = obj.GetType();

            if (!type.IsGenericType || !type.FullName.StartsWith("System.Tuple`") && !type.FullName.StartsWith("System.ValueTuple`"))
            {
                return(false);
            }

            var elemCount = type.GetGenericArguments().Length;

            if (elemCount != _expr.Expressions.Count)
            {
                return(false);
            }

            for (var i = 0; i < elemCount; i++)
            {
                var visitor = Visitor.For(_expr.Expressions[i]);
                var elem    = type.IsValueType
                    ? type.GetField("Item" + (i + 1)).GetValue(obj)
                    : type.GetProperty("Item" + (i + 1)).GetValue(obj);
                if (!visitor.Visit(elem, context))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        public override bool Visit(object obj, VisitorContext context)
        {
            if (obj == null)
            {
                return(false);
            }

            var type = obj.GetType();

            if (!type.IsArray)
            {
                return(false);
            }

            var length     = (int)type.GetProperty("Length").GetValue(obj);
            var elemGetter = type.GetMethod("Get");

            if (_expr.Expressions.Count != length)
            {
                return(false);
            }

            for (var i = 0; i < length; i++)
            {
                var elem    = elemGetter.Invoke(obj, new object[] { i });
                var visitor = Visitor.For(_expr.Expressions[i]);
                if (!visitor.Visit(elem, context))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #3
0
        public override bool Visit(object obj, VisitorContext context)
        {
            var nameExpr = _expr.Arguments[1] as ConstantExpression;
            var name     = nameExpr.Value as string;

            if (context.CapturedObjects.ContainsKey(name))
            {
                throw new ArgumentException($"Name '{name}' is already registered in the pattern!");
            }

            context.CapturedObjects[name] = obj;

            return(true);
        }
예제 #4
0
        public override bool Visit(object obj, VisitorContext context)
        {
            if (_expr.Value == null && obj == null)
            {
                return(true);
            }

            if (_expr.Value == null || obj == null)
            {
                return(false);
            }

            return(_expr.Value.Equals(obj));
        }
예제 #5
0
        public Option <TResult> Match(TValue value)
        {
            var context = new VisitorContext();
            var isMatch = Visitor.For(_expr.Body).Visit(value, context);

            if (!isMatch)
            {
                return(Option.None <TResult>());
            }

            var argExprs = new List <Expression>();

            foreach (var arg in _func.Method.GetParameters())
            {
                if (!context.CapturedObjects.TryGetValue(arg.Name, out var argValue))
                {
                    throw new ArgumentException($"No value was captured for argument '{arg.Name}'!");
                }

                var argType = arg.ParameterType;
                if (argValue == null)
                {
                    if (argType.IsValueType)
                    {
                        if (!argType.IsGenericType || argType.GetGenericTypeDefinition() != typeof(Nullable <>))
                        {
                            throw new ArgumentException($"Value for argument '{arg.Name}' must be {argType.Name}, found null!");
                        }
                    }
                }
                else
                {
                    var valueType = argValue.GetType();
                    if (!argType.IsAssignableFrom(valueType))
                    {
                        throw new ArgumentException($"Value for argument '{arg.Name}' must be {argType.Name}, found {valueType.Name}!");
                    }
                }

                argExprs.Add(Expression.Constant(argValue));
            }

            var callExpr   = Expression.Call(Expression.Constant(_func.Target), _func.Method, argExprs);
            var lambdaExpr = Expression.Lambda(callExpr);
            var result     = lambdaExpr.Compile().DynamicInvoke();

            return((Option <TResult>)result);
        }
예제 #6
0
        public override bool Visit(object obj, VisitorContext context)
        {
            if (obj == null)
            {
                return(false);
            }

            var type    = _expr.Method.GetGenericArguments()[0];
            var objType = obj.GetType();

            if (!type.IsAssignableFrom(objType))
            {
                return(false);
            }

            return(Visitor.For(_expr.Arguments[0]).Visit(obj, context));
        }
예제 #7
0
 public override bool Visit(object obj, VisitorContext context)
 {
     return(true);
 }