Created
April 22, 2013 07:23
-
-
Save kazuk/5433024 to your computer and use it in GitHub Desktop.
RazorEngineAndLinq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
using RazorEngine; | |
using RazorEngine.Compilation; | |
using RazorEngine.Compilation.Inspectors; | |
using RazorEngine.Templating; | |
using RazorEngine.Text; | |
namespace RazorEngineAndLinq | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
RazorEngine.Compilation.CompilerServiceBuilder.SetCompilerServiceFactory( | |
new CustomCompilerServiceFactory(RazorEngine.Compilation.CompilerServiceBuilder.GetDefaultCompilerService() ) | |
); | |
string template = @" | |
@using System.Linq; | |
Hello @Model.Name! Welcome to Razor! | |
@Enumerable.Range(1,10).Sum() | |
"; | |
string result = Razor.Parse(template, new {Name = "World"}); | |
Console.WriteLine(result); | |
Console.ReadLine(); | |
} | |
} | |
internal class CustomCompilerServiceFactory : ICompilerServiceFactory | |
{ | |
private readonly ICompilerService _defaultCompilerService; | |
public CustomCompilerServiceFactory(ICompilerService defaultCompilerService) | |
{ | |
_defaultCompilerService = defaultCompilerService; | |
} | |
public ICompilerService CreateCompilerService(Language language) | |
{ | |
return new CustomCompilerService(_defaultCompilerService); | |
} | |
} | |
internal class CustomCompilerService : ICompilerService | |
{ | |
private readonly ICompilerService _defaultCompilerService; | |
public CustomCompilerService(ICompilerService defaultCompilerService) | |
{ | |
_defaultCompilerService = defaultCompilerService; | |
} | |
public string BuildTypeName(Type templateType) | |
{ | |
return _defaultCompilerService.BuildTypeName(templateType); | |
} | |
public Tuple<Type, Assembly> CompileType(TypeContext context) | |
{ | |
return _defaultCompilerService.CompileType(context); | |
} | |
public IEnumerable<string> IncludeAssemblies() | |
{ | |
var includeAssemblies = _defaultCompilerService.IncludeAssemblies(); | |
return includeAssemblies.Concat( new[]{ "System.Core", "System.Linq" }); | |
} | |
public IEnumerable<ICodeInspector> CodeInspectors | |
{ | |
get { return _defaultCompilerService.CodeInspectors; } | |
set { _defaultCompilerService.CodeInspectors = value; } | |
} | |
public bool Debug | |
{ | |
get { return _defaultCompilerService.Debug; } | |
set { _defaultCompilerService.Debug = value; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment