diff --git a/src/BootstrapBlazor.Server/Startup.cs b/src/BootstrapBlazor.Server/Startup.cs index 0b7b6787770679b584278852891326a97ad8581a..b586f3fccd7bf2e6c66506d08d0960c2f0327914 100644 --- a/src/BootstrapBlazor.Server/Startup.cs +++ b/src/BootstrapBlazor.Server/Startup.cs @@ -54,6 +54,7 @@ namespace BootstrapBlazor.Server { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + services.AddLogging(); services.AddCors(); services.AddResponseCompression(); diff --git a/src/BootstrapBlazor/Utils/CacheManager.cs b/src/BootstrapBlazor/Utils/CacheManager.cs index 566d888a05ab35f099ac8df0780e4dc32bd034f8..3abe52bc3c7a9cf5a7114c15384987e2a508e208 100644 --- a/src/BootstrapBlazor/Utils/CacheManager.cs +++ b/src/BootstrapBlazor/Utils/CacheManager.cs @@ -3,7 +3,6 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using BootstrapBlazor.Localization.Json; -using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -36,8 +35,8 @@ namespace BootstrapBlazor.Components //$"Placeholder-{modelType.Name}-{fieldName}"; //$"GetProperty-{modelType.Name}-{fieldName}"; - //$"Lambda-{nameof(LambdaExtensions.GetPropertyValueLambda)}-{typeof(TModel).Name}-{fieldName}"; - //$"Lambda-{nameof(LambdaExtensions.SetPropertyValueLambda)}-{typeof(TModel).Name}-{fieldName}"; + //("Lambda-Get", model, fieldName); + //("Lambda-Set", model, fieldName, typeof(TValue)); //$"Lambda-{nameof(LambdaExtensions.GetSortLambda)}-{typeof(T).Name}"; //$"Lambda-{nameof(CreateConverterInvoker)}-{type.Name}"; @@ -252,13 +251,6 @@ namespace BootstrapBlazor.Components } #region Lambda Property - /// - /// - /// - /// - /// - /// - /// internal static bool TryGetProperty(Type modelType, string fieldName, [NotNullWhen(true)] out PropertyInfo? propertyInfo) { var cacheKey = $"GetProperty-{modelType.FullName}-{fieldName}"; @@ -285,7 +277,7 @@ namespace BootstrapBlazor.Components internal static TResult GetPropertyValue(TModel model, string fieldName) { var type = model is object o ? o.GetType() : typeof(TModel); - var cacheKey = ("Lambda-Get", model, fieldName); + var cacheKey = ("Lambda-Get", type, fieldName, typeof(TResult)); var invoker = CacheManager.GetOrCreate(cacheKey, entry => { entry.SetDynamicAssemblyPolicy(type); @@ -297,7 +289,7 @@ namespace BootstrapBlazor.Components internal static void SetPropertyValue(TModel model, string fieldName, TValue value) { var type = model is object o ? o.GetType() : typeof(TModel); - var cacheKey = ("Lambda-Set", model, fieldName, typeof(TValue)); + var cacheKey = ("Lambda-Set", type, fieldName, typeof(TValue)); var invoker = CacheManager.GetOrCreate(cacheKey, entry => { entry.SetDynamicAssemblyPolicy(type); @@ -325,8 +317,8 @@ namespace BootstrapBlazor.Components var cacheKey = $"Lambda-{nameof(CreateConverterInvoker)}-{type.FullName}"; return CacheManager.GetOrCreate(cacheKey, entry => { - var method = typeof(Utility) - .GetMethod(nameof(ConvertToString), BindingFlags.NonPublic | BindingFlags.Static)! + var method = typeof(CacheManager) + .GetMethod(nameof(CacheManager.ConvertToString), BindingFlags.NonPublic | BindingFlags.Static)! .MakeGenericMethod(type); var para_exp = Expression.Parameter(typeof(object)); @@ -336,11 +328,12 @@ namespace BootstrapBlazor.Components entry.SetDynamicAssemblyPolicy(type); return Expression.Lambda>>(body, para_exp).Compile(); - static IEnumerable ConvertToString(List source) => source is List list - ? list.Select(i => i.Value) - : source.Select(i => i?.ToString()); }); } + + private static IEnumerable ConvertToString(List source) => source is List list + ? list.Select(i => i.Value) + : source.Select(i => i?.ToString()); #endregion #region Format diff --git a/test/UnitTest/Performance/ReflectionTest.cs b/test/UnitTest/Performance/ReflectionTest.cs index 002d94f8815565db973903eccdaa87992180d231..39aee88991552847f300a67d3d65dec10a863760 100644 --- a/test/UnitTest/Performance/ReflectionTest.cs +++ b/test/UnitTest/Performance/ReflectionTest.cs @@ -2,12 +2,14 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using BootstrapBlazor.Components; using System; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using Xunit; using Xunit.Abstractions; +using static System.Net.Mime.MediaTypeNames; namespace UnitTest.Performance { @@ -108,14 +110,11 @@ namespace UnitTest.Performance sw.Stop(); Logger.WriteLine($"Reflection: {sw.Elapsed}"); - var target = Expression.Parameter(typeof(Dummy)); - Expression expression = Expression.Call(target, mi); - var func = Expression.Lambda>(expression, target).Compile(); - + var invoker = LambdaExtensions.GetPropertyValueLambda(s1, "Name").Compile(); sw = Stopwatch.StartNew(); for (var i = 0; i < count; i++) { - func.Invoke(s1); + invoker.Invoke(s1); } sw.Stop(); Logger.WriteLine($"Expression: {sw.Elapsed}"); @@ -127,14 +126,14 @@ namespace UnitTest.Performance { var test = new Dummy { Name = "Test" }; var count = 10000000; - var obj = LambdaExtensions.GetPropertyValue(test, "Name"); + var invoker = LambdaExtensions.GetPropertyValueLambda(test, "Name").Compile(); var stopWatch = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { - LambdaExtensions.GetPropertyValue(test, "Name"); + invoker(test); } stopWatch.Stop(); - Logger.WriteLine($"Expression: {stopWatch.ElapsedMilliseconds}"); + Logger.WriteLine($"Expression: {stopWatch.Elapsed}"); var objectType = test.GetType(); var method = objectType.GetProperty("Name")?.GetGetMethod(false); @@ -145,7 +144,7 @@ namespace UnitTest.Performance proxy(test); } stopWatch.Stop(); - Logger.WriteLine($"Delegate: {stopWatch.ElapsedMilliseconds}"); + Logger.WriteLine($"Delegate: {stopWatch.Elapsed}"); } private class Dummy