private void InterceptExecute(IInvocation invocation)
        {
            if (invocation.Method.Name != nameof(Keyword.Execute) && invocation.Method.Name != nameof(Keyword.Hydrate))
            {
                return;
            }

            foreach (var key in _lazyValues.Keys)
            {
                var newValue     = _lazyExpressionResolver.Resolve(_lazyValues[key]);
                var propertyInfo = invocation.TargetType.GetProperty(key);

                if (propertyInfo == null)
                {
                    throw new ArgumentException($"{invocation.TargetType} does not have {key} property.");
                }

                var propertyInfoSetMethod = propertyInfo.SetMethod;
                if (propertyInfo.SetMethod == null)
                {
                    throw new ArgumentException($"{propertyInfo.Name} has no Setter");
                }
                propertyInfoSetMethod.Invoke(invocation.InvocationTarget, new[] { newValue });
            }
            if (invocation.InvocationTarget is Keyword)
            {
                (invocation.InvocationTarget as Keyword).IsHydrated = true;
            }
            _lazyValues = new Dictionary <string, object>();
        }
Beispiel #2
0
        public IKeyword ParseKeyword(string command)
        {
            var option = FollowupOptions.FirstOrDefault(x => x.Matches(command));

            if (option != null)
            {
                return(option);
            }

            command = command.Trim();
            var template = Keywords.First(x => x.Matches(command));

            if (ServiceLocator.IsInitialised())
            {
                ServiceLocator.Instance.Resolve(template);
            }

            var parts = new string[]
            {
                command.Split(' ').First(),
                string.Join(" ", command.Split(' ').Skip(1))
            };

            if (parts.Length == 2 && _lazyExpressionResolver.CanResolve(parts.Last()))
            {
                var resolvedParameter = _lazyExpressionResolver.Resolve(parts.Last()) as string;

                var newInstance = CreateNewInstanceViaReflection(parts.First());

                newInstance.Instruction = /*parts.First() + ' ' +*/ resolvedParameter;

                return(newInstance);
            }

            //command = Normalize(command);

            var instance = CreateNewInstanceViaReflection(parts.First());

            if (!string.IsNullOrEmpty(parts.Last()))
            {
                instance.Instruction = parts.Last();
            }

            return(instance);
        }