Manos.Mvc.RazorViewCompiler.CompileView C# (CSharp) Method

CompileView() public method

public CompileView ( ) : Type
return System.Type
        public Type CompileView()
        {
            // generate code
            GeneratorResults results;
            using (var rdr = new StreamReader(ViewFile))
            {
                results =  GenerateViewCode(rdr);
            }

            // Check for errors
            if (!results.Success)
            {
                var x = new CompileException("Failed to parse/generate Razor View");
                foreach (var e in results.ParserErrors)
                {
                    x.AddError(ViewFile, e.Location.LineIndex + 1, e.Location.CharacterIndex + 1, e.Length, e.Message);
                }
                throw x;
            }

            // Compile the code dom
            var compileParams = new CompilerParameters()
            {
                GenerateInMemory = true,
                IncludeDebugInformation = true,
                GenerateExecutable = false,
                CompilerOptions = "/target:library /optimize"
            };

            // Add references to all currently loaded assemblies
            foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (!a.IsDynamic)
                    compileParams.ReferencedAssemblies.Add(a.Location);
            }

            // Compile
            var codeDom = (CodeDomProvider)Activator.CreateInstance(Language.CodeDomProviderType);
            var compileResults = codeDom.CompileAssemblyFromDom(compileParams, results.GeneratedCode);

            // Display compiler errors
            if (compileResults.Errors != null && compileResults.Errors.Count > 0)
            {
                var x = new CompileException("Failed to compile Razor view");
                foreach (CompilerError e in compileResults.Errors)
                {
                    x.AddError(ViewFile, e.Line, e.Column, 0, e.ErrorText);
                }
                throw x;
            }

            return compileResults.CompiledAssembly.GetType("Manos.Mvc.GeneratedView");
        }

Usage Example

Exemplo n.º 1
0
        public StartView CreateStartView()
        {
            // Compile the start view
            if (!start_view_prepared)
            {
                start_view_prepared = true;

                // Load the start view
                string start_view_file = Service.Application.MapPath("/Views/_ViewStart.cshtml");
                if (System.IO.File.Exists(start_view_file))
                {
                    var compiler = new RazorViewCompiler();
                    compiler.BaseClass = typeof(StartView);
                    compiler.ViewFile = start_view_file;
                    start_view_type = compiler.CompileView();
                }
            }

            // Do we have a start view?
            if (start_view_type == null)
                return null;

            // Create it
            return (StartView)Activator.CreateInstance(start_view_type);
        }
All Usage Examples Of Manos.Mvc.RazorViewCompiler::CompileView