Skip to content

Commit

Permalink
refactor(ALC): add assembly extension method GetUniqueName (#3285)
Browse files Browse the repository at this point in the history
* If it is an assembly in ALC, add a cache key condition

* refactor: 撤销代码格式化

* refactor: 撤销命名空间格式化

* refactor: 增加 AssemblyExtensions 提高代码复用率

* chore: bump version 8.4.5

---------

Co-authored-by: Argo-AscioTech <[email protected]>
  • Loading branch information
kimdiego2098 and ArgoZhang authored Apr 14, 2024
1 parent 6a0abbb commit 3276c05
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.4.4-beta01</Version>
<Version>8.4.5</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
19 changes: 19 additions & 0 deletions src/BootstrapBlazor/Extensions/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
// 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 System.Reflection;

namespace BootstrapBlazor.Components;

static class AssemblyExtensions
{
/// <summary>
/// 获得唯一类型名称方法
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public static string GetUniqueName(this Assembly assembly) => (assembly.IsCollectible
? $"{assembly.GetHashCode()}"
: $"{assembly.GetName().Name}");
}
4 changes: 3 additions & 1 deletion src/BootstrapBlazor/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ void EnsureNoAuthenticationSchemeSpecified()
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetUniqueTypeName(this Type type) => $"{type.FullName}-{type.TypeHandle.Value}";
public static string GetUniqueTypeName(this Type type) => (type.IsCollectible
? $"{type.FullName}-{type.TypeHandle.Value}"
: $"{type.FullName}");
}
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public override LocalizedString this[string name]
{
string? ret = null;
var cultureName = CultureInfo.CurrentUICulture.Name;
var cacheKey = $"{nameof(GetValueFromCache)}&name={name}&{Assembly.GetName().Name}&type={typeName}&culture={cultureName}";
var cacheKey = $"{nameof(GetValueFromCache)}&name={name}&{Assembly.GetUniqueName()}&type={typeName}&culture={cultureName}";
if (!CacheManager.GetMissingLocalizerByKey(cacheKey))
{
var l = GetLocalizedString();
Expand Down Expand Up @@ -133,7 +133,7 @@ public override LocalizedString this[string name]
{
string? ret = null;
var cultureName = CultureInfo.CurrentUICulture.Name;
var cacheKey = $"{nameof(GetLocalizerValueFromCache)}&name={name}&{Assembly.GetName().Name}&type={typeName}&culture={cultureName}";
var cacheKey = $"{nameof(GetLocalizerValueFromCache)}&name={name}&{Assembly.GetUniqueName()}&type={typeName}&culture={cultureName}";
if (!CacheManager.GetMissingLocalizerByKey(cacheKey))
{
var l = localizer[name];
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ void SetValue()
if (model != null)
{
var type = model.GetType();
var cacheKey = ($"Lambda-GetKeyValue-{type.GetUniqueTypeName()}-{customAttribute?.FullName}", typeof(TModel));
var cacheKey = ($"Lambda-GetKeyValue-{type.GetUniqueTypeName()}-{customAttribute?.GetUniqueTypeName()}", typeof(TModel));
var invoker = Instance.GetOrCreate(cacheKey, entry =>
{
entry.SetDynamicAssemblyPolicy(type);
Expand Down Expand Up @@ -615,7 +615,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
/// <returns></returns>
public static Func<TModel, ITableColumn, Func<TModel, ITableColumn, object?, Task>, object> GetOnValueChangedInvoke<TModel>(Type fieldType)
{
var cacheKey = $"Lambda-{nameof(GetOnValueChangedInvoke)}-{typeof(TModel).FullName}-{fieldType.GetUniqueTypeName()}";
var cacheKey = $"Lambda-{nameof(GetOnValueChangedInvoke)}-{typeof(TModel).GetUniqueTypeName()}-{fieldType.GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry =>
{
entry.SetDynamicAssemblyPolicy(fieldType);
Expand Down

0 comments on commit 3276c05

Please sign in to comment.