Skip to content

Commit e3b63e3

Browse files
committed
merge
2 parents d34ebce + a6e8e84 commit e3b63e3

14 files changed

+95
-72
lines changed

ExampleAppCore/ExampleAppCore.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
<ItemGroup>
1313
</ItemGroup>
1414

15+
<<<<<<< HEAD
1516
<ItemGroup>
1617
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1718
</ItemGroup>
19+
=======
20+
<ItemGroup>
21+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
22+
</ItemGroup>
23+
>>>>>>> a6e8e849f18a037f07982eed9acd2900900fabc1
1824

1925
<ItemGroup>
2026
<ProjectReference Include="..\RazorEngineCore\RazorEngineCore.csproj" />

ExampleAppNet472/ExampleAppNET472.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
9+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Install-Package RazorEngineCore
3030
* [🚀 Piotr Bakun: How to make PDF invoice with Playwright.NET](https://bakson.dev/2022/11/10/razor-engine-core.html)
3131
* [🚀 Christian Schou: How to send email with MailKit](https://blog.christian-schou.dk/send-emails-with-asp-net-core-with-mailkit/)
3232
* [🚀 Dave Glick: Difference between RazorEngineCore, RazorLight and Statiq, Send email using SendGrid API](https://www.twilio.com/blog/render-emails-using-razor-templating)
33-
* [🧑‍🎓 CodeProject: Building String Razor Template Engine with Bare Hands](https://www.codeproject.com/Articles/5260233/Building-String-Razor-Template-Engine-with-Bare-Ha)
33+
* [🧑‍🎓 Alexander Selishchev: Building String Razor Template Engine with Bare Hands](https://www.codeproject.com/Articles/5260233/Building-String-Razor-Template-Engine-with-Bare-Ha)
3434
* [📕 Razor syntax reference](https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-6.0)
3535

3636
## Wiki
@@ -230,18 +230,21 @@ IRazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(templateText
230230
});
231231

232232
string result = compiledTemplate.Run(new { name = "Hello" });
233-
``
233+
```
234234

235235
#### Credits
236236
This package is inspired by [Simon Mourier SO post](https://stackoverflow.com/a/47756437/267736)
237237

238238

239239
#### Changelog
240+
* 2023.11.2
241+
* Vertual keyword fixed
240242
* 2023.11.1
241243
* Template debugging 🚀 PR#111 (thanks [@shehrozeee](https://github.com/shehrozeee) and [@TheAtomicOption](https://github.com/TheAtomicOption))
242244
* Add CancellationToken overloads to Compile methods PR#134 (thanks [@daviddotcs](https://github.com/daviddotcs))
243245
* Package license information #133
244-
* Code cleanup
246+
* Code cleanup
247+
* ⚠️ Breaking change for @Raw, [update your code from wiki](https://github.com/adoconnection/RazorEngineCore/wiki/@Raw)
245248
* 2022.8.1
246249
* Proper namespace handling for nested types and types without namespace #113 (thanks [@Kirmiir](https://github.com/Kirmiir))
247250
* 2022.7.6

RazorEngineCore.Tests/RazorEngineCore.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net472;net5.0;net6.0</TargetFrameworks>
4+
<TargetFrameworks>net472;net6.0</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
11-
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
12-
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
12+
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

RazorEngineCore.Tests/TestCompileAndRun.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ public void TestCompileCancellation_DynamicModel()
739739

740740
Assert.ThrowsException<OperationCanceledException>(() =>
741741
{
742-
IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token);
742+
_ = razorEngine.Compile("Hello @Model.Name", cancellationToken: cancellationSource.Token);
743743
});
744744
}
745745
}
@@ -754,7 +754,7 @@ public async Task TestCompileCancellation_DynamicModelAsync()
754754

755755
await Assert.ThrowsExceptionAsync<OperationCanceledException>(async () =>
756756
{
757-
IRazorEngineCompiledTemplate template = await razorEngine.CompileAsync("Hello @Model.Name", cancellationToken: cancellationSource.Token);
757+
_ = await razorEngine.CompileAsync("Hello @Model.Name", cancellationToken: cancellationSource.Token);
758758
});
759759
}
760760
}
@@ -769,7 +769,7 @@ public void TestCompileCancellation_TypedModel1()
769769

770770
Assert.ThrowsException<OperationCanceledException>(() =>
771771
{
772-
IRazorEngineCompiledTemplate<TestTemplate1> template = razorEngine.Compile<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
772+
_ = razorEngine.Compile<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
773773
});
774774
}
775775
}
@@ -784,7 +784,7 @@ public async Task TestCompileCancellation_TypedModel1Async()
784784

785785
await Assert.ThrowsExceptionAsync<OperationCanceledException>(async () =>
786786
{
787-
IRazorEngineCompiledTemplate<TestTemplate1> template = await razorEngine.CompileAsync<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
787+
_ = await razorEngine.CompileAsync<TestTemplate1>("Hello @A @B @(A + B) @C @Decorator(\"777\")", cancellationToken: cancellationSource.Token);
788788
});
789789
}
790790
}

RazorEngineCore/AnonymousTypeWrapper.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
3232
return true;
3333
}
3434

35-
var type = result.GetType();
35+
//var type = result.GetType();
3636

3737
if (result.IsAnonymous())
3838
{
@@ -41,12 +41,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
4141

4242
if (result is IDictionary dictionary)
4343
{
44-
List<object> keys = new List<object>();
45-
46-
foreach(object key in dictionary.Keys)
47-
{
48-
keys.Add(key);
49-
}
44+
List<object> keys = dictionary.Keys.Cast<object>().ToList();
5045

5146
foreach(object key in keys)
5247
{
@@ -56,22 +51,13 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
5651
}
5752
}
5853
}
59-
else if (result is IEnumerable enumerable && !(result is string))
54+
else if (result is IEnumerable enumerable and not string)
6055
{
6156
result = enumerable.Cast<object>()
62-
.Select(e =>
63-
{
64-
if (e.IsAnonymous())
65-
{
66-
return new AnonymousTypeWrapper(e);
67-
}
68-
69-
return e;
70-
})
57+
.Select(e => e.IsAnonymous() ? new AnonymousTypeWrapper(e) : e)
7158
.ToList();
7259
}
73-
74-
60+
7561
return true;
7662
}
7763
}

RazorEngineCore/ObjectExtenders.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Reflection;
66
using System.Runtime.CompilerServices;
7+
using System.Threading.Tasks;
78

89
namespace RazorEngineCore
910
{
@@ -32,18 +33,25 @@ public static bool IsAnonymous(this object obj)
3233
&& type.Attributes.HasFlag(TypeAttributes.NotPublic);
3334
}
3435

35-
public static long ReadLong(this Stream stream)
36+
public static async Task<long> ReadLong(this Stream stream)
3637
{
3738
byte[] buffer = new byte[8];
38-
stream.Read(buffer, 0, 8);
39-
39+
#if NETSTANDARD2_0
40+
_ = await stream.ReadAsync(buffer, 0, buffer.Length);
41+
#else
42+
_ = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length));
43+
#endif
4044
return BitConverter.ToInt64(buffer, 0);
4145
}
4246

43-
public static void WriteLong(this Stream stream, long value)
47+
public static async Task WriteLong(this Stream stream, long value)
4448
{
4549
byte[] buffer = BitConverter.GetBytes(value);
46-
stream.Write(buffer, 0, buffer.Length);
50+
#if NETSTANDARD2_0
51+
await stream.WriteAsync(buffer, 0, buffer.Length);
52+
#else
53+
await stream.WriteAsync(buffer);
54+
#endif
4755
}
4856
}
4957
}

RazorEngineCore/RazorEngine.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public IRazorEngineCompiledTemplate<T> Compile<T>(string content, Action<IRazorE
3030

3131
public Task<IRazorEngineCompiledTemplate<T>> CompileAsync<T>(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, CancellationToken cancellationToken = default) where T : IRazorEngineTemplate
3232
{
33-
return Task.Factory.StartNew(() => this.Compile<T>(content: content, builderAction: builderAction, cancellationToken: cancellationToken));
33+
return Task.Run(() => this.Compile<T>(content: content, builderAction: builderAction, cancellationToken: cancellationToken));
3434
}
3535

3636
public IRazorEngineCompiledTemplate Compile(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, CancellationToken cancellationToken = default)
@@ -46,7 +46,7 @@ public IRazorEngineCompiledTemplate Compile(string content, Action<IRazorEngineC
4646

4747
public Task<IRazorEngineCompiledTemplate> CompileAsync(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, CancellationToken cancellationToken = default)
4848
{
49-
return Task.Factory.StartNew(() => this.Compile(
49+
return Task.Run(() => this.Compile(
5050
content,
5151
builderAction,
5252
cancellationToken));
@@ -105,7 +105,9 @@ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream(strin
105105
})
106106
.Concat(options.MetadataReferences)
107107
.ToList(),
108-
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
108+
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
109+
.WithOptimizationLevel(OptimizationLevel.Release)
110+
.WithOverflowChecks(true));
109111

110112

111113
MemoryStream assemblyStream = new MemoryStream();

RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ private string RenderTypeName(Type type)
6363

6464
string result = string.Join(".", elements.Where(e => !string.IsNullOrWhiteSpace(e)));
6565

66-
if (result.Contains('`'))
66+
int tildeLocation = result.IndexOf('`');
67+
if (tildeLocation > -1)
6768
{
68-
result = result.Substring(0, result.IndexOf("`"));
69+
result = result.Substring(0, tildeLocation);
6970
}
7071

7172
if (type.GenericTypeArguments.Length == 0)

RazorEngineCore/RazorEngineCompiledTemplate.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public static RazorEngineCompiledTemplate LoadFromFile(string fileName)
2222

2323
public static async Task<RazorEngineCompiledTemplate> LoadFromFileAsync(string fileName)
2424
{
25+
#if NETSTANDARD2_0
2526
using (FileStream fileStream = new FileStream(
26-
path: fileName,
27-
mode: FileMode.Open,
28-
access: FileAccess.Read,
29-
share: FileShare.None,
30-
bufferSize: 4096,
31-
useAsync: true))
27+
#else
28+
await using (FileStream fileStream = new FileStream(
29+
#endif
30+
path: fileName,
31+
mode: FileMode.Open,
32+
access: FileAccess.Read,
33+
share: FileShare.None,
34+
bufferSize: 4096,
35+
useAsync: true))
3236
{
3337
return await LoadFromStreamAsync(fileStream);
3438
}

0 commit comments

Comments
 (0)