Skip to content

Commit 2677e92

Browse files
committed
feat: 添加特性缓存
1 parent 372269e commit 2677e92

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/BootstrapBlazor/Components/BaseComponents/BootstrapModuleComponentBase.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6-
using System.Reflection;
7-
86
namespace BootstrapBlazor.Components;
97

108
/// <summary>
@@ -82,13 +80,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
8280
protected virtual void OnLoadJSModule()
8381
{
8482
var type = this.GetType();
85-
var inherited = type.GetCustomAttribute<JSModuleNotInheritedAttribute>() == null;
86-
if (inherited)
83+
if (CacheManager.GetAttribute<JSModuleNotInheritedAttribute>(type) == null)
8784
{
88-
var attributes = type.GetCustomAttributes<JSModuleAutoLoaderAttribute>();
89-
if (attributes.Any())
85+
var attr = CacheManager.GetAttribute<JSModuleAutoLoaderAttribute>(type);
86+
if (attr != null)
9087
{
91-
var attr = attributes.First();
9288
AutoInvokeDispose = attr.AutoInvokeDispose;
9389
AutoInvokeInit = attr.AutoInvokeInit;
9490

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ private List<ICacheEntry> GetAllValues(MemoryCache cache)
216216
}
217217
#endif
218218

219+
#region Attribute
220+
/// <summary>
221+
/// 获得类型特性标签方法
222+
/// </summary>
223+
/// <param name="type"></param>
224+
/// <returns></returns>
225+
public static Attr? GetAttribute<Attr>(Type type) where Attr : Attribute
226+
{
227+
return Instance.GetOrCreate((type, typeof(Attr)), _ =>
228+
{
229+
return type.GetCustomAttribute<Attr>();
230+
});
231+
}
232+
233+
#endregion
234+
219235
#region Assembly
220236
/// <summary>
221237
/// 获得唯一类型名称方法

0 commit comments

Comments
 (0)