-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDomainModelList.cs
81 lines (77 loc) · 2.66 KB
/
DomainModelList.cs
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
using SurveyJSAsFormLibrary.Attributes;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace SurveyJSAsFormLibrary.DomainModels
{
public class DomainModelInfo
{
public string Name { get; set; }
public Type Type { get; set; }
public string Title { get; set; }
}
public class DomainModelList
{
private static Dictionary<string, DomainModelInfo> domainsValue;
private static Dictionary<string, DomainModelInfo> domains
{
get {
if(domainsValue == null)
{
domainsValue = new Dictionary<string, DomainModelInfo>();
loadDomainList();
}
return domainsValue;
}
}
private static void loadDomainList()
{
Assembly domainAssembly = Assembly.GetExecutingAssembly();
foreach (Type type in domainAssembly.GetTypes())
{
if (!typeof(DomainModel).IsAssignableFrom(type)) continue;
foreach (CustomAttributeData attr in type.CustomAttributes)
{
if (attr.AttributeType == typeof(DomainModelFormAttribute))
{
string name = (string)attr.ConstructorArguments[0].Value;
string title = (string)attr.ConstructorArguments[1].Value;
domainsValue[name] = new DomainModelInfo() { Name = name, Type = type, Title = getTitle(name, title) };
}
}
}
}
private static string getTitle(string name, string title)
{
if (!string.IsNullOrEmpty(title)) return title;
string[] words = name.Split("-");
string res = "";
foreach(string word in words)
{
res += char.ToUpper(word[0]) + word.Substring(1) + ' ';
}
return res + "Form";
}
public static Type GetTypeByFormName(string name)
{
DomainModelInfo res;
domains.TryGetValue(name, out res);
return res != null ? res.Type : null;
}
public static string GetTitleByFormName(string name)
{
DomainModelInfo res;
domains.TryGetValue(name, out res);
return res != null ? res.Title : name;
}
public static IList<DomainModelInfo> GetAllForms()
{
var list = new List<DomainModelInfo>();
foreach(DomainModelInfo info in domains.Values)
{
list.Add(info);
}
return list;
}
}
}