-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSupport.cs
More file actions
165 lines (159 loc) · 8.55 KB
/
Copy pathSupport.cs
File metadata and controls
165 lines (159 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Microsoft.AspNetCore.Http;
using System.Diagnostics;
using System.Net.Http;
using System.Reflection;
using UISupportGeneric.UI;
namespace UISupportBlazor
{
/// <summary>
/// GUI support features
/// </summary>
public static class Support
{
/// <summary>
/// Get all ClassInfos instantiated as members of the type passed as a parameter.
/// This function also creates a browsing session for the current user(if it hasn't already been created), and creates an object of the type passed as a parameter and assigns it to the user session.
/// </summary>
/// <param name="httpContext">Current httpContext</param>
/// <param name="panelsType">The type of object that represents the navigation menu, that is, an object with properties, each of which represents a panel associated with a menu item.</param>
/// <returns>The list of ClassInfos that represent the panels that the application must have. This list corresponds to the properties of the type passed as a parameter.</returns>
public static List<ClassInfo>? GetAllClassInfo(HttpContext httpContext, Type panelsType)
{
List<ClassInfo>? classInfoList = null;
if (httpContext != null)
{
lock (httpContext)
{
var session = Session.Sessions.GetSession(httpContext);
object panels;
if (session.Values.TryGetValue(nameof(panels), out panels))
{
if (session.Values.TryGetValue(nameof(classInfoList), out object? classInfoListObj))
{
return (List<ClassInfo>)classInfoListObj;
}
}
else
{
try
{
panels = Activator.CreateInstance(panelsType);
}
catch (Exception)
{
throw new Exception("Cannot process classes without parameterless constructor (invalid type value)");
}
session.Values.Add(nameof(panels), panels);
classInfoList = [];
session.Values.Add(nameof(classInfoList), classInfoList);
var classInfo = UISupportGeneric.Util.GetClassInfo(panels);
foreach (var element in classInfo.Elements)
{
if (element is ClassInfo info)
classInfoList.Add(info);
}
}
}
}
return classInfoList;
}
/// <summary>
/// Get all ClassInfos instantiated as members of the type passed as a parameter.
/// This function also creates a browsing session for the current user(if it hasn't already been created), and creates an object of the type passed as a parameter and assigns it to the user session.
/// </summary>
/// <param name="httpContextAccessor">http Context Accessor object</param>
/// <param name="atNamespace">The namespace where all the classes representing the panels are defined. The default value indicates the namespace associated with the Panels directory</param>
/// <returns></returns>
public static List<ClassInfo>? GetAllClassInfo(IHttpContextAccessor httpContextAccessor, string? atNamespace = default)
{
if (!Session.IsConfigured)
Session.Configure(httpContextAccessor);
return GetAllClassInfo(httpContext: null, atNamespace);
}
/// <summary>
/// Get all ClassInfos instantiated as members of the type passed as a parameter.
/// This function also creates a browsing session for the current user(if it hasn't already been created), and creates an object of the type passed as a parameter and assigns it to the user session.
/// </summary>
/// <param name="httpContext">Current httpContext</param>
/// <param name="atNamespace">The namespace where all the classes representing the panels are defined. The default value indicates the namespace associated with the Panels directory</param>
/// <returns>The list of ClassInfos that represent the panels that the application must have. This list corresponds to the properties of the type passed as a parameter.</returns>
public static List<ClassInfo>? GetAllClassInfo(HttpContext? httpContext = null, string? atNamespace = default)
{
httpContext ??= Session.GetCurrentHttpContext();
List<ClassInfo>? classInfoList = null;
if (httpContext != null)
{
Assembly? assembly = null;
if (atNamespace != null)
assembly = UISupportGeneric.Util.NamespaceToAssembly(atNamespace);
if (assembly == null)
{
var currentAssembly = MethodBase.GetCurrentMethod().DeclaringType.Assembly;
var stackTrace = new StackTrace();
for (int i = 0; i < stackTrace.FrameCount; i++)
{
var method = stackTrace.GetFrame(i)?.GetMethod();
var declaringType = method?.DeclaringType;
assembly = declaringType?.Assembly;
if (assembly != null && assembly != currentAssembly)
{
break;
}
}
}
if (atNamespace == null)
{
var fullNamespace = UISupportGeneric.Util.GetNamespace(assembly);
if (fullNamespace == null)
return null;
var rootNamespace = fullNamespace.Split('.')[0];
atNamespace = rootNamespace + ".Panels";
}
lock (httpContext)
{
var classes = assembly.GetTypes().Where(t => t.Namespace == atNamespace && t.IsClass && t.IsPublic);
var session = Session.Sessions.GetSession(httpContext);
object panels;
if (session != null)
{
lock (session)
{
if (!session.Values.TryGetValue(nameof(panels), out panels))
{
var panelsDictionary = new Dictionary<string, object>();
classInfoList = [];
foreach (var type in classes)
{
if (UISupportGeneric.Util.IsStaticClass(type))
{
panelsDictionary.Add(type.Name, type);
var classInfo = UISupportGeneric.Util.GetClassInfo(type);
classInfoList.Add(classInfo);
}
else if (type.GetConstructor(Type.EmptyTypes) != null)
{
var panel = Activator.CreateInstance(type);
if (panel != null)
{
panelsDictionary.Add(type.Name, panel);
var classInfo = UISupportGeneric.Util.GetClassInfo(panel);
classInfoList.Add(classInfo);
}
}
}
panels = panelsDictionary;
session.Values.Add(nameof(panels), panelsDictionary); // The panel dictionary contains the type, for static classes, and the class instance for non-static classes.
session.Values.Add(nameof(classInfoList), classInfoList);
}
else if (session.Values.TryGetValue(nameof(classInfoList), out object? classInfoListObj))
{
return (List<ClassInfo>)classInfoListObj;
}
}
}
}
}
return classInfoList;
}
}
}