コード例 #1
0
        public void NameOfConstEvaluatorTest(string textExpression, string expectedValue)
        {
            var walker = new ConstantExpressionSyntaxEvaluator <string>();
            var exp    = SyntaxTreeHelper.GetExpressionSyntax($"nameof({textExpression})");

            var value = walker.Visit(exp);

            Assert.Equal(expectedValue, value);
        }
コード例 #2
0
        public void SimpleStringConstEvaluatorTest()
        {
            var textValue = "MyValue";
            var walker    = new ConstantExpressionSyntaxEvaluator <string>();
            var exp       = SyntaxTreeHelper.GetExpressionSyntax($@"""{textValue}""");

            var value = walker.Visit(exp);

            Assert.Equal(textValue, value);
        }
コード例 #3
0
        public bool TryMatchRepeatDeclaration(AttributeSyntax repeatAttributeSyntax, string expression)
        {
            var constEvaluator = new ConstantExpressionSyntaxEvaluator <string>();
            var patternName    = constEvaluator.Visit(repeatAttributeSyntax.ArgumentList.Arguments.First().Expression);

            // get the property from the current pattern generic definition.
            var repeatProperty = this.pattern.Properties.First(p => p.Name == patternName);

            return(AutomatedPropertyStrategy.Match(repeatProperty, expression));
        }
コード例 #4
0
        public void RepeatDeclaration(
            AttributeSyntax repeatAttributeSyntax,
            Action <IAutomatedStrategy> callback)
        {
            var constEvaluator = new ConstantExpressionSyntaxEvaluator <string>();
            var patternName    = constEvaluator.Visit(repeatAttributeSyntax.ArgumentList.Arguments.First().Expression);

            var repeatPattern = this.resolver.Resolve(patternName, this.pattern);

            foreach (var declaration in this.declarations)
            {
                callback(
                    new AutomatedGenericStrategy(
                        (IGenericDeclaration <SyntaxNode>)repeatPattern,
                        (IGenericDeclaration <SyntaxNode>)declaration,
                        this.resolver));
            }
        }
コード例 #5
0
        private ISelector GetSelectorFromPatternAttribute(AttributeSyntax attributeSyntax)
        {
            var selectorExp = attributeSyntax.ArgumentList.Arguments.First();

            var constEvaluator   = new ConstantExpressionSyntaxEvaluator <string>();
            var selectorTypeName = constEvaluator.Visit(selectorExp.Expression);

            var nsBase = this.pattern.UsingDirectives
                         .Concat(NameSpaceHelper.GetParentNameSpaces(this.pattern.DeclarationNameSpace));

            foreach (var usingDirective in nsBase)
            {
                var selectorType = Type.GetType($"{usingDirective}.{selectorTypeName}");
                if (selectorType != null)
                {
                    return((ISelector)Activator.CreateInstance(selectorType));
                }
            }

            throw new ArgumentException($"Unknown selector {selectorTypeName}");
        }
コード例 #6
0
        public void RepeatDeclaration(
            AttributeSyntax repeatAttributeSyntax,
            Action <IAutomatedStrategy> callback)
        {
            var constEvaluator = new ConstantExpressionSyntaxEvaluator <string>();
            var patternName    = constEvaluator.Visit(repeatAttributeSyntax.ArgumentList.Arguments.First().Expression);

            var resolved = this.resolver.Resolve(patternName, this.pattern);

            // Check if this is self repeat pattern reference.
            if (object.ReferenceEquals(this.pattern, resolved))
            {
                callback(this);
                return;
            }

            // get the property from the current pattern generic definition.
            var repeatProperty = this.pattern.Properties.First(p => p.Name == patternName);

            ISelector selector;

            // Get the selector if any from the matching property.
            if (repeatProperty.SyntaxNodeProvider.SyntaxNode.AttributeLists
                .TryMatchAttributeName <PatternAttribute>(out var attributeSyntax))
            {
                selector = this.GetSelectorFromPatternAttribute(attributeSyntax);
            }
            else
            {
                selector = new AllPropertySelector();
            }

            foreach (var propertyDeclaration in selector.GetProperties(this.declaration))
            {
                var strategy = new AutomatedPropertyStrategy(repeatProperty, propertyDeclaration);

                callback(strategy);
            }
        }