diff --git a/ExampleAppCore/ExampleAppCore.csproj b/ExampleAppCore/ExampleAppCore.csproj index 5d95f99..5c23f19 100644 --- a/ExampleAppCore/ExampleAppCore.csproj +++ b/ExampleAppCore/ExampleAppCore.csproj @@ -2,6 +2,7 @@ Exe + latest netcoreapp3.1 @@ -12,10 +13,6 @@ - - - - diff --git a/ExampleAppCore/Program.cs b/ExampleAppCore/Program.cs index ac46bce..e62a6e2 100644 --- a/ExampleAppCore/Program.cs +++ b/ExampleAppCore/Program.cs @@ -1,25 +1,147 @@ using System; using System.Collections.Generic; +using System.IO; +using System.IO.Pipelines; +using System.Text; +using System.Threading.Tasks; using RazorEngineCore; namespace ExampleApp { - public class TestModel : RazorEngineTemplateBase + public abstract class BinaryDataEngineTemplateBase : BinaryDataEngineTemplateBase + { + public new T Model { get; set; } + } + + public class BinaryDataEngineTemplateBase : IRazorEngineTemplate + { + readonly MemoryStream binaryWriter = new(); + + Encoding encoding = Encoding.UTF8; + + private string attributeSuffix = null; + + public dynamic Model { get; set; } + + public void WriteLiteral(string literal = null) + { + WriteLiteralAsync(literal).GetAwaiter().GetResult(); + } + + public virtual Task WriteLiteralAsync(string literal = null) + { + Write(literal); + return Task.CompletedTask; + } + + public void Write(object obj = null) + { + WriteAsync(obj).GetAwaiter().GetResult(); + } + + public virtual Task WriteAsync(object obj = null) + { + if (obj is byte[] bytes) + binaryWriter.Write(bytes); + else + binaryWriter.Write( encoding.GetBytes(obj.ToString())); + return Task.CompletedTask; + } + + public void BeginWriteAttribute(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, + int attributeValuesCount) + { + BeginWriteAttributeAsync(name, prefix, prefixOffset, suffix, suffixOffset, attributeValuesCount).GetAwaiter().GetResult(); + } + + public virtual Task BeginWriteAttributeAsync(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount) + { + this.attributeSuffix = suffix; + Write(prefix); + return Task.CompletedTask; + } + + public void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, + bool isLiteral) + { + WriteAttributeValueAsync(prefix, prefixOffset, value, valueOffset, valueLength, isLiteral).GetAwaiter().GetResult(); + } + + public virtual Task WriteAttributeValueAsync(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral) + { + Write(prefix); + Write(value); + return Task.CompletedTask; + } + + public void EndWriteAttribute() + { + EndWriteAttributeAsync().GetAwaiter().GetResult(); + } + + public virtual Task EndWriteAttributeAsync() + { + Write(attributeSuffix); + this.attributeSuffix = null; + return Task.CompletedTask; + } + + public void Execute() + { + ExecuteAsync().GetAwaiter().GetResult(); + } + + public virtual Task ExecuteAsync() + { + return Task.CompletedTask; + } + + public virtual byte[] Result() + { + return ResultAsync().GetAwaiter().GetResult(); + } + + public virtual Task ResultAsync() + { + return Task.FromResult( binaryWriter.ToArray() ); + } + } + + public class TestModel : BinaryDataEngineTemplateBase { public string Name { get; set; } - public IEnumerable Items { get; set; } + public IEnumerable Items { get; set; } + public byte[] BinaryData { get; set; } } + class Program { - static string Content = @" + static readonly string Content = @" Hello @Model.Name +@{ + string testheader = "" ---------------- mytest: will show the modified items ----------- ""; + List myitems = new (); + foreach (var item in Model.Items) { + myitems.Add("" ---->>>> "" + item); + } +} + @foreach(var item in @Model.Items) {
- @item
} +@testheader +@foreach(var myitem in @myitems) +{ + | @(myitem) | @(myitem) | @(myitem) | + +} +Binary data below: +@Model.BinaryData +End of Binary data
@@ -42,19 +164,32 @@ void RecursionTest(int level){ static void Main(string[] args) { IRazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile(Content); + var template = razorEngine.Compile, byte[]>(Content); + + /* + StringBuilder stringBuilder = new StringBuilder(); + byte[] somebytes = { 0x02, 0x04, 0x50 }; + stringBuilder.Append(somebytes); + + var s = stringBuilder.ToString(); + */ - string result = template.Run(new + TestModel model = new TestModel() { Name = "Alexander", Items = new List() { "item 1", "item 2" - } - }); + }, + BinaryData = new byte[10] { 0x41, 0x0A, 0x0D, 0x00, 0x30, 0x31, 0x00, 0x0A, 0x0D, 0x41 } + }; + + + + byte[] result = template.Run(instance => instance.Model = model); - Console.WriteLine(result); + Console.WriteLine(Encoding.UTF8.GetString(result)); Console.ReadKey(); } } diff --git a/ExampleAppNET5/Program.cs b/ExampleAppNET5/Program.cs index 3fde7ed..99ccafb 100644 --- a/ExampleAppNET5/Program.cs +++ b/ExampleAppNET5/Program.cs @@ -38,7 +38,7 @@ void RecursionTest(int level){ static void Main(string[] args) { IRazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile(Content); + IRazorEngineCompiledTemplate template = razorEngine.Compile(Content); string result = template.Run(new { diff --git a/ExampleAppNet472/Program.cs b/ExampleAppNet472/Program.cs index f9651ee..d392c85 100644 --- a/ExampleAppNet472/Program.cs +++ b/ExampleAppNet472/Program.cs @@ -45,7 +45,7 @@ void RecursionTest(int level){ static void Main(string[] args) { IRazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile(Content); + IRazorEngineCompiledTemplate template = razorEngine.Compile(Content); string result = template.Run( new diff --git a/RazorEngineCore.Tests/Models/TestTemplate1.cs b/RazorEngineCore.Tests/Models/TestTemplate1.cs index 9a63623..9719927 100644 --- a/RazorEngineCore.Tests/Models/TestTemplate1.cs +++ b/RazorEngineCore.Tests/Models/TestTemplate1.cs @@ -3,7 +3,7 @@ namespace RazorEngineCore.Tests.Models { - public class TestTemplate1 : RazorEngineTemplateBase + public class TestTemplate1 : RazorEngineTemplateBase { public int A { get; set; } public int B { get; set; } diff --git a/RazorEngineCore.Tests/TestCompileAndRun.cs b/RazorEngineCore.Tests/TestCompileAndRun.cs index 3f868b0..616cb46 100644 --- a/RazorEngineCore.Tests/TestCompileAndRun.cs +++ b/RazorEngineCore.Tests/TestCompileAndRun.cs @@ -35,7 +35,7 @@ public Task TestCompileAsync() public void TestCompileAndRun_HtmlLiteral() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile("

Hello @Model.Name

"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("

Hello @Model.Name

"); string actual = template.Run(new { @@ -49,7 +49,7 @@ public void TestCompileAndRun_HtmlLiteral() public async Task TestCompileAndRun_HtmlLiteralAsync() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("

Hello @Model.Name

"); + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("

Hello @Model.Name

"); string actual = await template.RunAsync(new { @@ -63,7 +63,7 @@ public async Task TestCompileAndRun_HtmlLiteralAsync() public void TestCompileAndRun_InAttributeVariables() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile("
"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("
"); string actual = template.Run(new { @@ -77,7 +77,7 @@ public void TestCompileAndRun_InAttributeVariables() public void TestCompileAndRun_InAttributeVariables2() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile(""); + IRazorEngineCompiledTemplate template = razorEngine.Compile(""); string actual = template.Run(new { @@ -91,7 +91,7 @@ public void TestCompileAndRun_InAttributeVariables2() public async Task TestCompileAndRun_InAttributeVariablesAsync() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("
"); + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("
"); string actual = await template.RunAsync(new { @@ -105,7 +105,7 @@ public async Task TestCompileAndRun_InAttributeVariablesAsync() public void TestCompileAndRun_HtmlAttribute() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile("
Hello
"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("
Hello
"); string actual = template.Run(new { @@ -119,7 +119,7 @@ public void TestCompileAndRun_HtmlAttribute() public async Task TestCompileAndRun_HtmlAttributeAsync() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("
Hello
"); + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("
Hello
"); string actual = await template.RunAsync(new { @@ -133,7 +133,7 @@ public async Task TestCompileAndRun_HtmlAttributeAsync() public void TestCompileAndRun_DynamicModel_Plain() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name"); string actual = template.Run(new { @@ -147,7 +147,7 @@ public void TestCompileAndRun_DynamicModel_Plain() public async Task TestCompileAndRun_DynamicModel_PlainAsync() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @Model.Name"); + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @Model.Name"); string actual = await template.RunAsync(new { @@ -218,7 +218,7 @@ public void TestCompileAndRun_NullablePropertyWithValue() DateTime? dateTime = DateTime.Now; - IRazorEngineCompiledTemplate template = razorEngine.Compile("DateTime: @Model.DateTime.Value.ToString()"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("DateTime: @Model.DateTime.Value.ToString()"); string actual = template.Run(instance => instance.Model = new TestModel() { @@ -235,7 +235,7 @@ public void TestCompileAndRun_NullablePropertyWithoutValue() DateTime? dateTime = null; - IRazorEngineCompiledTemplate template = razorEngine.Compile("DateTime: @Model.DateTime"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("DateTime: @Model.DateTime"); string actual = template.Run(instance => instance.Model = new TestModel() { @@ -459,7 +459,7 @@ void RecursionTest(int level) public void TestCompileAndRun_TypedModel1() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @A @B @(A + B) @C @Decorator(\"777\")"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @A @B @(A + B) @C @Decorator(\"777\")"); string actual = template.Run(instance => { @@ -475,7 +475,7 @@ public void TestCompileAndRun_TypedModel1() public async Task TestCompileAndRun_TypedModel1Async() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @A @B @(A + B) @C @Decorator(\"777\")"); + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @A @B @(A + B) @C @Decorator(\"777\")"); string actual = await template.RunAsync(instance => { @@ -491,7 +491,7 @@ public async Task TestCompileAndRun_TypedModel1Async() public void TestCompileAndRun_TypedModel2() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Decorator(Model.C)"); + IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Decorator(Model.C)"); string actual = template.Run(instance => { @@ -513,7 +513,7 @@ Hello @Model.Decorator(Model.C) "; RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate> template = razorEngine.Compile>(templateText); + IRazorEngineCompiledTemplate, string> template = razorEngine.Compile>(templateText); string actual = template.Run(instance => { @@ -530,7 +530,7 @@ Hello @Model.Decorator(Model.C) public async Task TestCompileAndRun_TypedModel2Async() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @Model.Decorator(Model.C)"); + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @Model.Decorator(Model.C)"); string actual = await template.RunAsync(instance => { @@ -547,7 +547,7 @@ public async Task TestCompileAndRun_TypedModel2Async() public void TestCompileAndRun_AnonymousModelWithArrayOfObjects() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile( + IRazorEngineCompiledTemplate template = razorEngine.Compile( @" @foreach (var item in Model.Numbers.OrderByDescending(x => x)) { @@ -575,7 +575,7 @@ public void TestCompileAndRun_AnonymousModelWithArrayOfObjects() public void TestCompileAndRun_StronglyTypedModelLinq() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile( + IRazorEngineCompiledTemplate template = razorEngine.Compile( @" @foreach (var item in Model.Numbers.OrderByDescending(x => x)) { @@ -602,7 +602,7 @@ public void TestCompileAndRun_StronglyTypedModelLinq() public void TestCompileAndRun_DynamicModelLinq() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = razorEngine.Compile( + IRazorEngineCompiledTemplate template = razorEngine.Compile( @" @foreach (var item in ((IEnumerable)Model.Numbers).OrderByDescending(x => x)) { @@ -626,7 +626,7 @@ public void TestCompileAndRun_DynamicModelLinq() public async Task TestCompileAndRun_LinqAsync() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync( + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync( @" @foreach (var item in Model.Numbers.OrderByDescending(x => x)) { @@ -693,7 +693,7 @@ public static string GetGreeting(string name) : null; RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync(@" + IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync(@" @using TestAssembly

@Greeting.GetGreeting(""Name"")

", builder => diff --git a/RazorEngineCore.Tests/TestSaveLoad.cs b/RazorEngineCore.Tests/TestSaveLoad.cs index db3ffb0..11fa0cd 100644 --- a/RazorEngineCore.Tests/TestSaveLoad.cs +++ b/RazorEngineCore.Tests/TestSaveLoad.cs @@ -11,13 +11,13 @@ public class TestSaveLoad public void TestSaveToStream() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("Hello @Model.Name"); + IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("Hello @Model.Name"); MemoryStream memoryStream = new MemoryStream(); initialTemplate.SaveToStream(memoryStream); memoryStream.Position = 0; - IRazorEngineCompiledTemplate loadedTemplate = RazorEngineCompiledTemplate.LoadFromStream(memoryStream); + IRazorEngineCompiledTemplate loadedTemplate = RazorEngineCompiledTemplate.LoadFromStream(memoryStream); string initialTemplateResult = initialTemplate.Run(new { Name = "Alex" }); string loadedTemplateResult = loadedTemplate.Run(new { Name = "Alex" }); @@ -29,13 +29,13 @@ public void TestSaveToStream() public async Task TestSaveToStreamAsync() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate initialTemplate = await razorEngine.CompileAsync("Hello @Model.Name"); + IRazorEngineCompiledTemplate initialTemplate = await razorEngine.CompileAsync("Hello @Model.Name"); MemoryStream memoryStream = new MemoryStream(); await initialTemplate.SaveToStreamAsync(memoryStream); memoryStream.Position = 0; - IRazorEngineCompiledTemplate loadedTemplate = await RazorEngineCompiledTemplate.LoadFromStreamAsync(memoryStream); + IRazorEngineCompiledTemplate loadedTemplate = await RazorEngineCompiledTemplate.LoadFromStreamAsync(memoryStream); string initialTemplateResult = await initialTemplate.RunAsync(new { Name = "Alex" }); string loadedTemplateResult = await loadedTemplate.RunAsync(new { Name = "Alex" }); @@ -47,11 +47,11 @@ public async Task TestSaveToStreamAsync() public void TestSaveToFile() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("Hello @Model.Name"); + IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("Hello @Model.Name"); initialTemplate.SaveToFile("testTemplate.dll"); - IRazorEngineCompiledTemplate loadedTemplate = RazorEngineCompiledTemplate.LoadFromFile("testTemplate.dll"); + IRazorEngineCompiledTemplate loadedTemplate = RazorEngineCompiledTemplate.LoadFromFile("testTemplate.dll"); string initialTemplateResult = initialTemplate.Run(new { Name = "Alex" }); string loadedTemplateResult = loadedTemplate.Run(new { Name = "Alex" }); @@ -63,11 +63,11 @@ public void TestSaveToFile() public async Task TestSaveToFileAsync() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate initialTemplate = await razorEngine.CompileAsync("Hello @Model.Name"); + IRazorEngineCompiledTemplate initialTemplate = await razorEngine.CompileAsync("Hello @Model.Name"); await initialTemplate.SaveToFileAsync("testTemplate.dll"); - IRazorEngineCompiledTemplate loadedTemplate = await RazorEngineCompiledTemplate.LoadFromFileAsync("testTemplate.dll"); + IRazorEngineCompiledTemplate loadedTemplate = await RazorEngineCompiledTemplate.LoadFromFileAsync("testTemplate.dll"); string initialTemplateResult = await initialTemplate.RunAsync(new { Name = "Alex" }); string loadedTemplateResult = await loadedTemplate.RunAsync(new { Name = "Alex" }); diff --git a/RazorEngineCore.Tests/TestTemplateFilename.cs b/RazorEngineCore.Tests/TestTemplateFilename.cs index f0a5634..47c8c71 100644 --- a/RazorEngineCore.Tests/TestTemplateFilename.cs +++ b/RazorEngineCore.Tests/TestTemplateFilename.cs @@ -15,7 +15,7 @@ public void TestSettingTemplateFilename() var errorThrown = false; try { - IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("@{ this is a syntaxerror }", + IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("@{ this is a syntaxerror }", builder => { builder.Options.TemplateFilename = "templatefilenameset.txt"; }); } catch (Exception e) diff --git a/RazorEngineCore.Tests/TestTemplateNamespace.cs b/RazorEngineCore.Tests/TestTemplateNamespace.cs index 908ec1e..670d060 100644 --- a/RazorEngineCore.Tests/TestTemplateNamespace.cs +++ b/RazorEngineCore.Tests/TestTemplateNamespace.cs @@ -14,7 +14,7 @@ public void TestSettingTemplateNamespace() { RazorEngine razorEngine = new RazorEngine(); - IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("@{ var message = \"OK\"; }@message", + IRazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("@{ var message = \"OK\"; }@message", builder => { builder.Options.TemplateNamespace = "Test.Namespace"; }); var result = initialTemplate.Run(); diff --git a/RazorEngineCore/IRazorEngine.cs b/RazorEngineCore/IRazorEngine.cs index 3de2203..dfd2717 100644 --- a/RazorEngineCore/IRazorEngine.cs +++ b/RazorEngineCore/IRazorEngine.cs @@ -1,18 +1,22 @@ using System; +using System.IO.Pipelines; using System.Threading.Tasks; namespace RazorEngineCore { public interface IRazorEngine { - IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) - where T : IRazorEngineTemplate; + IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) + where T : IRazorEngineTemplate; + IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) + where T : IRazorEngineTemplate; - Task> CompileAsync(string content, Action builderAction = null) - where T : IRazorEngineTemplate; + Task> CompileAsync(string content, Action builderAction = null) + where T : IRazorEngineTemplate; - IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null); + IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null); - Task CompileAsync(string content, Action builderAction = null); + Task> CompileAsync(string content, Action builderAction = null); + } } \ No newline at end of file diff --git a/RazorEngineCore/IRazorEngineCompiledTemplate.cs b/RazorEngineCore/IRazorEngineCompiledTemplate.cs index a08808d..66ab0f2 100644 --- a/RazorEngineCore/IRazorEngineCompiledTemplate.cs +++ b/RazorEngineCore/IRazorEngineCompiledTemplate.cs @@ -3,13 +3,13 @@ namespace RazorEngineCore { - public interface IRazorEngineCompiledTemplate + public interface IRazorEngineCompiledTemplate { void SaveToStream(Stream stream); Task SaveToStreamAsync(Stream stream); void SaveToFile(string fileName); Task SaveToFileAsync(string fileName); - string Run(object model = null); - Task RunAsync(object model = null); + R Run(object model = null); + Task RunAsync(object model = null); } } \ No newline at end of file diff --git a/RazorEngineCore/IRazorEngineCompiledTemplateT.cs b/RazorEngineCore/IRazorEngineCompiledTemplateT.cs index 37cdf75..77fc865 100644 --- a/RazorEngineCore/IRazorEngineCompiledTemplateT.cs +++ b/RazorEngineCore/IRazorEngineCompiledTemplateT.cs @@ -4,13 +4,13 @@ namespace RazorEngineCore { - public interface IRazorEngineCompiledTemplate where T : IRazorEngineTemplate + public interface IRazorEngineCompiledTemplate where T : IRazorEngineTemplate { void SaveToStream(Stream stream); Task SaveToStreamAsync(Stream stream); void SaveToFile(string fileName); Task SaveToFileAsync(string fileName); - string Run(Action initializer); - Task RunAsync(Action initializer); + R Run(Action initializer); + Task RunAsync(Action initializer); } } \ No newline at end of file diff --git a/RazorEngineCore/IRazorEngineTemplate.cs b/RazorEngineCore/IRazorEngineTemplate.cs index eae4bb1..521ca17 100644 --- a/RazorEngineCore/IRazorEngineTemplate.cs +++ b/RazorEngineCore/IRazorEngineTemplate.cs @@ -2,7 +2,7 @@ namespace RazorEngineCore { - public interface IRazorEngineTemplate + public interface IRazorEngineTemplate { dynamic Model { get; set; } void WriteLiteral(string literal = null); @@ -29,8 +29,8 @@ public interface IRazorEngineTemplate Task ExecuteAsync(); - string Result(); + T Result(); - Task ResultAsync(); + Task ResultAsync(); } } \ No newline at end of file diff --git a/RazorEngineCore/RazorEngine.cs b/RazorEngineCore/RazorEngine.cs index ef31494..55a2c52 100644 --- a/RazorEngineCore/RazorEngine.cs +++ b/RazorEngineCore/RazorEngine.cs @@ -14,7 +14,7 @@ namespace RazorEngineCore { public class RazorEngine : IRazorEngine { - public IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) where T : IRazorEngineTemplate + public IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) where T : IRazorEngineTemplate { IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder(); @@ -25,15 +25,29 @@ public IRazorEngineCompiledTemplate Compile(string content, Action(memoryStream, compilationOptionsBuilder.Options.TemplateNamespace); + return new RazorEngineCompiledTemplate(memoryStream, compilationOptionsBuilder.Options.TemplateNamespace); } - public Task> CompileAsync(string content, Action builderAction = null) where T : IRazorEngineTemplate + public IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) where T : IRazorEngineTemplate + { + IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder(); + + compilationOptionsBuilder.AddAssemblyReference(typeof(T).Assembly); + compilationOptionsBuilder.Inherits(typeof(T)); + + builderAction?.Invoke(compilationOptionsBuilder); + + MemoryStream memoryStream = this.CreateAndCompileToStream(content, compilationOptionsBuilder.Options); + + return new RazorEngineCompiledTemplate(memoryStream, compilationOptionsBuilder.Options.TemplateNamespace); + } + + public Task> CompileAsync(string content, Action builderAction = null) where T : IRazorEngineTemplate { return Task.Factory.StartNew(() => this.Compile(content: content, builderAction: builderAction)); } - public IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) + public IRazorEngineCompiledTemplate Compile(string content, Action builderAction = null) { IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder(); compilationOptionsBuilder.Inherits(typeof(RazorEngineTemplateBase)); @@ -42,10 +56,10 @@ public IRazorEngineCompiledTemplate Compile(string content, Action(memoryStream, compilationOptionsBuilder.Options.TemplateNamespace); } - public Task CompileAsync(string content, Action builderAction = null) + public Task> CompileAsync(string content, Action builderAction = null) { return Task.Factory.StartNew(() => this.Compile(content: content, builderAction: builderAction)); } @@ -103,13 +117,13 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R .ToList(), new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - MemoryStream memoryStream = new MemoryStream(); + MemoryStream memoryStream = new(); EmitResult emitResult = compilation.Emit(memoryStream); if (!emitResult.Success) { - RazorEngineCompilationException exception = new RazorEngineCompilationException() + RazorEngineCompilationException exception = new() { Errors = emitResult.Diagnostics.ToList(), GeneratedCode = razorCSharpDocument.GeneratedCode @@ -125,7 +139,7 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R protected virtual string WriteDirectives(string content, RazorEngineCompilationOptions options) { - StringBuilder stringBuilder = new StringBuilder(); + StringBuilder stringBuilder = new(); stringBuilder.AppendLine($"@inherits {options.Inherits}"); foreach (string entry in options.DefaultUsings) @@ -137,5 +151,7 @@ protected virtual string WriteDirectives(string content, RazorEngineCompilationO return stringBuilder.ToString(); } + + } } \ No newline at end of file diff --git a/RazorEngineCore/RazorEngineCompiledTemplate.cs b/RazorEngineCore/RazorEngineCompiledTemplate.cs index 9cf1df5..f331da7 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplate.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplate.cs @@ -5,7 +5,7 @@ namespace RazorEngineCore { - public class RazorEngineCompiledTemplate : IRazorEngineCompiledTemplate + public class RazorEngineCompiledTemplate : IRazorEngineCompiledTemplate { private readonly MemoryStream assemblyByteCode; private readonly Type templateType; @@ -18,16 +18,16 @@ internal RazorEngineCompiledTemplate(MemoryStream assemblyByteCode, string templ this.templateType = assembly.GetType(templateNamespace + ".Template"); } - public static IRazorEngineCompiledTemplate LoadFromFile(string fileName, string templateNamespace = "TemplateNamespace") + public static IRazorEngineCompiledTemplate LoadFromFile(string fileName, string templateNamespace = "TemplateNamespace") { return LoadFromFileAsync(fileName, templateNamespace).GetAwaiter().GetResult(); } - public static async Task LoadFromFileAsync(string fileName, string templateNamespace = "TemplateNamespace") + public static async Task> LoadFromFileAsync(string fileName, string templateNamespace = "TemplateNamespace") { - MemoryStream memoryStream = new MemoryStream(); + MemoryStream memoryStream = new(); - using (FileStream fileStream = new FileStream( + using (FileStream fileStream = new( path: fileName, mode: FileMode.Open, access: FileAccess.Read, @@ -38,21 +38,21 @@ public static async Task LoadFromFileAsync(string await fileStream.CopyToAsync(memoryStream); } - return new RazorEngineCompiledTemplate(memoryStream, templateNamespace); + return new RazorEngineCompiledTemplate(memoryStream, templateNamespace); } - public static IRazorEngineCompiledTemplate LoadFromStream(Stream stream) + public static IRazorEngineCompiledTemplate LoadFromStream(Stream stream) { return LoadFromStreamAsync(stream).GetAwaiter().GetResult(); } - public static async Task LoadFromStreamAsync(Stream stream, string templateNamespace = "TemplateNamespace") + public static async Task> LoadFromStreamAsync(Stream stream, string templateNamespace = "TemplateNamespace") { - MemoryStream memoryStream = new MemoryStream(); + MemoryStream memoryStream = new(); await stream.CopyToAsync(memoryStream); memoryStream.Position = 0; - return new RazorEngineCompiledTemplate(memoryStream, templateNamespace); + return new RazorEngineCompiledTemplate(memoryStream, templateNamespace); } public void SaveToStream(Stream stream) @@ -72,31 +72,29 @@ public void SaveToFile(string fileName) public Task SaveToFileAsync(string fileName) { - using (FileStream fileStream = new FileStream( + using FileStream fileStream = new( path: fileName, mode: FileMode.OpenOrCreate, access: FileAccess.Write, share: FileShare.None, bufferSize: 4096, - useAsync: true)) - { - return assemblyByteCode.CopyToAsync(fileStream); - } + useAsync: true); + return assemblyByteCode.CopyToAsync(fileStream); } - public string Run(object model = null) + public R Run(object model = null) { return this.RunAsync(model).GetAwaiter().GetResult(); } - public async Task RunAsync(object model = null) + public async Task RunAsync(object model = null) { if (model != null && model.IsAnonymous()) { model = new AnonymousTypeWrapper(model); } - IRazorEngineTemplate instance = (IRazorEngineTemplate) Activator.CreateInstance(this.templateType); + IRazorEngineTemplate instance = (IRazorEngineTemplate) Activator.CreateInstance(this.templateType); instance.Model = model; await instance.ExecuteAsync(); diff --git a/RazorEngineCore/RazorEngineCompiledTemplateT.cs b/RazorEngineCore/RazorEngineCompiledTemplateT.cs index 34229c9..02dbe6d 100644 --- a/RazorEngineCore/RazorEngineCompiledTemplateT.cs +++ b/RazorEngineCore/RazorEngineCompiledTemplateT.cs @@ -5,7 +5,7 @@ namespace RazorEngineCore { - public class RazorEngineCompiledTemplate : IRazorEngineCompiledTemplate where T : IRazorEngineTemplate + public class RazorEngineCompiledTemplate : IRazorEngineCompiledTemplate where T : IRazorEngineTemplate { private readonly MemoryStream assemblyByteCode; private readonly Type templateType; @@ -25,9 +25,9 @@ public static IRazorEngineCompiledTemplate LoadFromFile(string fileName, stri public static async Task> LoadFromFileAsync(string fileName, string templateNamespace = "TemplateNamespace") { - MemoryStream memoryStream = new MemoryStream(); + MemoryStream memoryStream = new(); - using (FileStream fileStream = new FileStream( + using (FileStream fileStream = new( path: fileName, mode: FileMode.Open, access: FileAccess.Read, @@ -48,7 +48,7 @@ public static IRazorEngineCompiledTemplate LoadFromStream(Stream stream) public static async Task> LoadFromStreamAsync(Stream stream, string templateNamespace = "TemplateNamespace") { - MemoryStream memoryStream = new MemoryStream(); + MemoryStream memoryStream = new(); await stream.CopyToAsync(memoryStream); memoryStream.Position = 0; @@ -72,24 +72,22 @@ public void SaveToFile(string fileName) public Task SaveToFileAsync(string fileName) { - using (FileStream fileStream = new FileStream( - path: fileName, - mode: FileMode.OpenOrCreate, + using FileStream fileStream = new( + path: fileName, + mode: FileMode.OpenOrCreate, access: FileAccess.Write, share: FileShare.None, - bufferSize: 4096, - useAsync: true)) - { - return assemblyByteCode.CopyToAsync(fileStream); - } + bufferSize: 4096, + useAsync: true); + return assemblyByteCode.CopyToAsync(fileStream); } - public string Run(Action initializer) + public R Run(Action initializer) { return this.RunAsync(initializer).GetAwaiter().GetResult(); } - public async Task RunAsync(Action initializer) + public async Task RunAsync(Action initializer) { T instance = (T) Activator.CreateInstance(this.templateType); initializer(instance); diff --git a/RazorEngineCore/RazorEngineCore.csproj b/RazorEngineCore/RazorEngineCore.csproj index 7eaf1b5..7f560c5 100644 --- a/RazorEngineCore/RazorEngineCore.csproj +++ b/RazorEngineCore/RazorEngineCore.csproj @@ -1,5 +1,6 @@  + latest net6.0;net5.0;netstandard2.0 true 2022.1.2 @@ -22,5 +23,6 @@ + diff --git a/RazorEngineCore/RazorEngineTemplateBase.cs b/RazorEngineCore/RazorEngineTemplateBase.cs index b54e570..b34c598 100644 --- a/RazorEngineCore/RazorEngineTemplateBase.cs +++ b/RazorEngineCore/RazorEngineTemplateBase.cs @@ -3,9 +3,9 @@ namespace RazorEngineCore { - public abstract class RazorEngineTemplateBase : IRazorEngineTemplate + public abstract class RazorEngineTemplateBase : IRazorEngineTemplate { - private readonly StringBuilder stringBuilder = new StringBuilder(); + private readonly StringBuilder stringBuilder = new(); private string attributeSuffix = null;