Skip to content

Commit 9c49d58

Browse files
committed
add cache for refection
1 parent 0027c2c commit 9c49d58

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

ant.mgr/mgr.core/App_Start/Attribute.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Reflection;
@@ -26,27 +27,39 @@ public APIAttribute(string name)
2627
}
2728

2829

30+
internal class APIMetaData
31+
{
32+
public string ClassName { get; set; }
33+
public Type Type { get; set; }
34+
public APIAttribute Attribute { get; set; }
35+
}
2936

3037
public class APIAttibuteHelper
3138
{
39+
40+
private static ConcurrentDictionary<Assembly,List<APIMetaData>> _cache = new ConcurrentDictionary<Assembly, List<APIMetaData>>();
41+
3242
//获取当前的程序集里面所有继承了BaseController的类
3343
//获取当前class上打了API标签的属性 + className
3444
//在获取当前class里面的所有打了API标签的method + methodName
3545
public static List<APIDescription> GetAllDescriptions(Assembly current = null,List<SystemPageAction> pageActions =null)
3646
{
3747
if (current == null) current = typeof(APIAttibuteHelper).Assembly;
3848
var result = new List<APIDescription>();
39-
var types = current.GetExportedTypes();
40-
var maps = (from t in types
41-
where t.IsClass && t.BaseType == typeof(BaseController) &&
42-
!t.IsAbstract && !t.IsInterface
43-
select new
44-
{
45-
ClassName = t.Name,
46-
Type = t,
47-
Attribute = t.GetCustomAttribute<APIAttribute>()
48-
}).ToArray();
49-
49+
if (!_cache.TryGetValue(current, out var maps))
50+
{
51+
var types = current.GetExportedTypes();
52+
maps = (from t in types
53+
where t.IsClass && t.BaseType == typeof(BaseController) &&
54+
!t.IsAbstract && !t.IsInterface
55+
select new APIMetaData
56+
{
57+
ClassName = t.Name,
58+
Type = t,
59+
Attribute = t.GetCustomAttribute<APIAttribute>()
60+
}).ToList();
61+
_cache.TryAdd(current, maps);
62+
}
5063
var div = new Dictionary<string,List<string>>();
5164
if (pageActions != null)
5265
{

0 commit comments

Comments
 (0)