Exemple #1
0
 public void WriteGenericConstraint(TrampolineContext context, CSharpCodeWriter writer)
 {
     writer.Write("where ");
     writer.WriteIdentifier(GenericParameterName);
     writer.Write(" : unmanaged, ");
     writer.Using(ConstraintInterfaceNamespace);
     writer.WriteIdentifier(ConstraintInterfaceTypeName);
     writer.WriteLine();
 }
Exemple #2
0
    public override void WriteOutputArgument(TrampolineContext context, CSharpCodeWriter writer)
    {
        writer.Write('(');
        context.WriteType(OutputType);
        writer.Write(')');

        if (ByRefKind is not null)
        {
            writer.WriteIdentifier(TemporaryName);
        }
        else
        {
            writer.WriteIdentifier(Name);
        }
    }
        protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
        {
            // Grab the last model chunk so it gets intellisense.
            var modelChunk = ChunkHelper.GetModelChunk(Context.ChunkTreeBuilder.ChunkTree);

            Model = modelChunk != null ? modelChunk.ModelType : _defaultModel;

            // If there were any model chunks then we need to modify the class declaration signature.
            if (modelChunk != null)
            {
                writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName));

                var modelVisitor = new ModelChunkVisitor(writer, Context);
                // This generates the base class signature
                modelVisitor.Accept(modelChunk);

                writer.WriteLine();

                return(new CSharpCodeWritingScope(writer));
            }
            else
            {
                return(base.BuildClassDeclaration(writer));
            }
        }
        /// <inheritdoc />
        /// <remarks>If the attribute being rendered is of the type
        /// <see cref="GeneratedTagHelperAttributeContext.ModelExpressionTypeName"/>, then a model expression will be
        /// created by calling into <see cref="GeneratedTagHelperAttributeContext.CreateModelExpressionMethodName"/>.
        /// </remarks>
        public override void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeDescriptor,
                                                  [NotNull] CSharpCodeWriter writer,
                                                  [NotNull] CodeBuilderContext codeBuilderContext,
                                                  [NotNull] Action<CSharpCodeWriter> renderAttributeValue,
                                                  bool complexValue)
        {
            if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal))
            {
                writer
                    .WriteStartMethodInvocation(_context.CreateModelExpressionMethodName)
                    .Write(ModelLambdaVariableName)
                    .Write(" => ");
                if (!complexValue)
                {
                    writer
                        .Write(ModelLambdaVariableName)
                        .Write(".");

                }

                renderAttributeValue(writer);

                writer.WriteEndMethodInvocation(endLine: false);
            }
            else
            {
                base.RenderAttributeValue(
                    attributeDescriptor,
                    writer,
                    codeBuilderContext,
                    renderAttributeValue,
                    complexValue);
            }
        }
Exemple #5
0
        protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
        {
            // Grab the last model chunk so it gets intellisense.
            // NOTE: If there's more than 1 model chunk there will be a Razor error BUT we want intellisense to
            // show up on the current model chunk that the user is typing.
            var modelChunk = Context.CodeTreeBuilder.CodeTree.Chunks.OfType <ModelChunk>()
                             .LastOrDefault();

            Model = modelChunk != null ? modelChunk.ModelType : _hostOptions.DefaultModel;

            // If there were any model chunks then we need to modify the class declaration signature.
            if (modelChunk != null)
            {
                writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName));

                var modelVisitor = new ModelChunkVisitor(writer, Context);
                // This generates the base class signature
                modelVisitor.Accept(modelChunk);

                writer.WriteLine();

                return(new CSharpCodeWritingScope(writer));
            }
            else
            {
                return(base.BuildClassDeclaration(writer));
            }
        }
Exemple #6
0
    public override bool WriteBlockBeforeCall(TrampolineContext context, CSharpCodeWriter writer)
    {
        if (ByRefKind is not ByRefKind byRefKind)
        {
            return(false);
        }

        writer.Write("fixed (");
        context.WriteType(TemporaryType);
        writer.Write(' ');
        writer.WriteIdentifier(TemporaryName);
        writer.Write(" = ");
        writer.Write('&');
        writer.WriteIdentifier(Name);
        writer.WriteLine(')');
        return(true);
    }
            public override void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeInfo,
                                                      [NotNull] CSharpCodeWriter writer,
                                                      [NotNull] CodeBuilderContext context,
                                                      [NotNull] Action <CSharpCodeWriter> renderAttributeValue)
            {
                writer.Write("**From custom attribute code renderer**: ");

                base.RenderAttributeValue(attributeInfo, writer, context, renderAttributeValue);
            }
        /// <inheritdoc />
        /// <remarks>If the attribute being rendered is of the type
        /// <see cref="GeneratedTagHelperAttributeContext.ModelExpressionTypeName"/>, then a model expression will be
        /// created by calling into <see cref="GeneratedTagHelperAttributeContext.CreateModelExpressionMethodName"/>.
        /// </remarks>
        public override void RenderAttributeValue(
            TagHelperAttributeDescriptor attributeDescriptor,
            CSharpCodeWriter writer,
            CodeGeneratorContext codeGeneratorContext,
            Action <CSharpCodeWriter> renderAttributeValue,
            bool complexValue)
        {
            if (attributeDescriptor == null)
            {
                throw new ArgumentNullException(nameof(attributeDescriptor));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (codeGeneratorContext == null)
            {
                throw new ArgumentNullException(nameof(codeGeneratorContext));
            }

            if (renderAttributeValue == null)
            {
                throw new ArgumentNullException(nameof(renderAttributeValue));
            }

            if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal))
            {
                writer
                .WriteStartInstanceMethodInvocation(_context.ModelExpressionProviderPropertyName, _context.CreateModelExpressionMethodName)
                .Write(_context.ViewDataPropertyName)
                .WriteParameterSeparator()
                .Write(ModelLambdaVariableName)
                .Write(" => ");
                if (!complexValue)
                {
                    writer
                    .Write(ModelLambdaVariableName)
                    .Write(".");
                }

                renderAttributeValue(writer);

                writer.WriteEndMethodInvocation(endLine: false);
            }
            else
            {
                base.RenderAttributeValue(
                    attributeDescriptor,
                    writer,
                    codeGeneratorContext,
                    renderAttributeValue,
                    complexValue);
            }
        }
Exemple #9
0
            public override void RenderAttributeValue(
                TagHelperAttributeDescriptor attributeInfo,
                CSharpCodeWriter writer,
                CodeGeneratorContext context,
                Action <CSharpCodeWriter> renderAttributeValue,
                bool complexValue)
            {
                writer.Write("**From custom attribute code renderer**: ");

                base.RenderAttributeValue(attributeInfo, writer, context, renderAttributeValue, complexValue);
            }
        public static CSharpCodeWriter WriteLineNumberDirective(this CSharpCodeWriter writer, MappingLocation location, string file)
        {
            if (location.FilePath != null)
            {
                file = location.FilePath;
            }

            if (writer.Builder.Length >= writer.NewLine.Length && !writer.IsAfterNewLine)
            {
                writer.WriteLine();
            }

            var lineNumberAsString = (location.LineIndex + 1).ToString(CultureInfo.InvariantCulture);

            return(writer.Write("#line ").Write(lineNumberAsString).Write(" \"").Write(file).WriteLine("\""));
        }
Exemple #11
0
        protected override void BuildConstructor(CSharpCodeWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            writer.WriteLineHiddenDirective();

            var injectVisitor = new InjectChunkVisitor(writer, Context, "Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute");

            injectVisitor.Accept(Context.ChunkTreeBuilder.Root.Children);

            var modelVisitor = new ModelChunkVisitor(writer, Context);

            modelVisitor.Accept(Context.ChunkTreeBuilder.Root.Children);
            if (modelVisitor.ModelType != null)
            {
                writer.WriteLine();

                // public ModelType Model => ViewData?.Model ?? default(ModelType);
                writer.Write("public ").Write(modelVisitor.ModelType).Write(" Model => ViewData?.Model ?? default(").Write(modelVisitor.ModelType).Write(");");
            }

            writer.WriteLine();
            var modelType = modelVisitor.ModelType ?? Context.ClassName;

            // public new ViewDataDictionary<Model> ViewData => (ViewDataDictionary<Model>)base.ViewData;
            var viewDataType = $"global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary<{modelType}>";

            writer.Write("public new ").Write(viewDataType).Write($" ViewData => ({viewDataType})base.ViewData;");

            // [RazorInject]
            // public IHtmlHelper<Model> Html { get; private set; }
            writer.WriteLine();
            writer.Write("[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]").WriteLine();
            writer.Write($"public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<{modelType}> Html {{ get; private set; }}").WriteLine();

            // [RazorInject]
            // public ILogger<PageClass> Logger { get; private set; }
            writer.WriteLine();
            writer.Write("[Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]").WriteLine();
            writer.Write($"public global::Microsoft.Extensions.Logging.ILogger<{Context.ClassName}> Logger {{ get; private set; }}").WriteLine();

            writer.WriteLine();
            writer.WriteLineHiddenDirective();
        }
        private void CSharpWriter(String original, String generated, String compare)
        {
            // copy "original" to ouput directory
            FileInfo file1 = new FileInfo("..\\..\\src\\Test\\CodeDomMerge\\" + original);

            file1.CopyTo(".\\SanityTest\\CodeDomMerge\\" + original, true);

            // get handle to file
            file1 = new FileInfo(".\\SanityTest\\CodeDomMerge\\" + original);
            FileInfo file2 = new FileInfo("..\\..\\src\\Test\\CodeDomMerge\\" + generated);
            FileInfo file3 = new FileInfo("..\\..\\src\\Test\\CodeDomMerge\\" + compare);

            // write/merge files
            CSharpCodeWriter writer = new CSharpCodeWriter();

            writer.Write(file1, file2.OpenText().ReadToEnd(), new NoStyler());

            // make sure new file matches compare file
            if (!file3.OpenText().ReadToEnd().Equals(file1.OpenText().ReadToEnd()))
            {
                Console.Out.WriteLine(Environment.NewLine + file1.OpenText().ReadToEnd());
                Assert.Fail();
            }
        }