/// <summary>
        /// Generates CSharp code for the specified <paramref name="fileInfoCollection"/>.
        /// </summary>
        /// <param name="fileInfoCollection">The <see cref="RazorFileInfoCollection"/>.</param>
        /// <returns></returns>
        public static string GenerateCode(RazorFileInfoCollection fileInfoCollection)
        {
            if (fileInfoCollection == null)
            {
                throw new ArgumentNullException(nameof(fileInfoCollection));
            }

            var builder = new StringBuilder();

            builder.Append(
$@"namespace __ASP_ASSEMBLY
{{
    [{typeof(CompilerGeneratedAttribute).FullName}]
    public class __PreGeneratedViewCollection : {typeof(RazorFileInfoCollection).FullName}
    {{
        public __PreGeneratedViewCollection()
        {{
            {nameof(RazorFileInfoCollection.AssemblyResourceName)} = @""{fileInfoCollection.AssemblyResourceName}"";
            {nameof(RazorFileInfoCollection.SymbolsResourceName)} = @""{fileInfoCollection.SymbolsResourceName}"";
            FileInfos = new System.Collections.Generic.List<{typeof(RazorFileInfo).FullName}>
            {{");

            foreach (var fileInfo in fileInfoCollection.FileInfos)
            {
                builder.Append(
$@"             
                new {typeof(RazorFileInfo).FullName}
                {{
                    {nameof(RazorFileInfo.FullTypeName)} = @""{fileInfo.FullTypeName}"",
                    {nameof(RazorFileInfo.RelativePath)} = @""{fileInfo.RelativePath}""
                }},");
            }

            builder.Append(
$@"
            }};
        }}

        private static {typeof(System.Reflection.Assembly).FullName} _loadedAssembly;

        public override {typeof(System.Reflection.Assembly).FullName} LoadAssembly(
            {typeof(IAssemblyLoadContext).FullName} loadContext)
        {{
             if (_loadedAssembly == null)
             {{
                _loadedAssembly = base.LoadAssembly(loadContext);
             }}
             return _loadedAssembly;   
        }}
    }}
}}");
            return builder.ToString();
        }
        public void GenerateCollection_ProducesExpectedCode(RazorFileInfoCollection collection, string expected)
        {
            // Act
            var actual = RazorFileInfoCollectionGenerator.GenerateCode(collection);

            // Assert
            Assert.Equal(expected, actual);
        }
Exemple #3
0
 public RazorFileInfoCollectionGenerator([NotNull] RazorFileInfoCollection fileInfoCollection,
                                         [NotNull] CompilationSettings compilationSettings)
 {
     RazorFileInfoCollection = fileInfoCollection;
     CompilationSettings     = compilationSettings;
 }