|
1 | 1 | using System;
|
| 2 | +using System.Collections.Concurrent; |
2 | 3 | using System.Collections.Generic;
|
3 | 4 | using System.Linq;
|
4 | 5 | using System.Reflection;
|
@@ -26,27 +27,39 @@ public APIAttribute(string name)
|
26 | 27 | }
|
27 | 28 |
|
28 | 29 |
|
| 30 | + internal class APIMetaData |
| 31 | + { |
| 32 | + public string ClassName { get; set; } |
| 33 | + public Type Type { get; set; } |
| 34 | + public APIAttribute Attribute { get; set; } |
| 35 | + } |
29 | 36 |
|
30 | 37 | public class APIAttibuteHelper
|
31 | 38 | {
|
| 39 | + |
| 40 | + private static ConcurrentDictionary<Assembly,List<APIMetaData>> _cache = new ConcurrentDictionary<Assembly, List<APIMetaData>>(); |
| 41 | + |
32 | 42 | //获取当前的程序集里面所有继承了BaseController的类
|
33 | 43 | //获取当前class上打了API标签的属性 + className
|
34 | 44 | //在获取当前class里面的所有打了API标签的method + methodName
|
35 | 45 | public static List<APIDescription> GetAllDescriptions(Assembly current = null,List<SystemPageAction> pageActions =null)
|
36 | 46 | {
|
37 | 47 | if (current == null) current = typeof(APIAttibuteHelper).Assembly;
|
38 | 48 | 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 | + } |
50 | 63 | var div = new Dictionary<string,List<string>>();
|
51 | 64 | if (pageActions != null)
|
52 | 65 | {
|
|
0 commit comments