示例#1
0
        protected override void Visit(ExpressionChunk chunk)
        {
            var expressionPiece = new CSharpSource
            {
                Code = chunk.Code,
            };

            _context.Builder.Add(expressionPiece);
        }
        private void Walk(ICSharpSource source)
        {
            var csharpBlock = source as CSharpBlock;

            if (csharpBlock != null)
            {
                for (var i = 0; i < csharpBlock.Children.Count; i++)
                {
                    Walk(csharpBlock.Children[i]);
                }
            }
            else if (source is SetTagHelperProperty)
            {
                var setProperty = (SetTagHelperProperty)source;
                if (!string.Equals(setProperty.AssociatedDescriptor.TypeName, _attributeLiterals.ModelExpressionTypeName, StringComparison.Ordinal))
                {
                    return;
                }

                var csharpPrefix = new StringBuilder();
                csharpPrefix
                .Append(_attributeLiterals.ModelExpressionProviderPropertyName)
                .Append(".")
                .Append(_attributeLiterals.CreateModelExpressionMethodName)
                .Append("(")
                .Append(_attributeLiterals.ViewDataPropertyName)
                .Append(", __model => ");

                if (setProperty.Value.Children.Count == 1 && setProperty.Value.Children.First() is RenderHtml)
                {
                    // Simple attribute value
                    csharpPrefix.Append("__model.");
                }

                // TODO: Code smell, this is mutating the tree. Should we be replacing entirely?
                var csharpPrefixElement = new CSharpSource {
                    Code = csharpPrefix.ToString()
                };
                setProperty.Value.Children.Insert(0, csharpPrefixElement);

                var csharpSuffixElement = new CSharpSource {
                    Code = ")"
                };
                setProperty.Value.Children.Add(csharpSuffixElement);
            }
        }