Datei: NDOEnhancer/NDOEnhancer/Enhancer/Nodes/AssemblyNode.cs

Last Commit (1c149c1)
1 //
2 // Copyright (c) 2002-2022 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Linq;
25 using System.Collections.Generic;
26 using System.Reflection;
27 using NDO;
28 using NDO.Mapping;
29 using System.Runtime.Versioning;
30 using NDO.Mapping.Attributes;
31
32 namespace NDOEnhancer
33 {
34 ····/// <summary>
35 ····/// Represents some properties of assemblies.
36 ····/// </summary>
37 ····internal class AssemblyNode
38 ····{
39
40 ········NDOMapping mappings;
41 ········
42 ········bool isEnhanced;
43 ········public bool IsEnhanced
44 ········{
45 ············get { return isEnhanced; }
46 ········}
47 ········string oidTypeName;
48 ········public string OidTypeName
49 ········{
50 ············get { return oidTypeName; }
51 ········}
52 ········Type oidType;
53 ········public Type OidType
54 ········{
55 ············get { return oidType; }
56 ········}
57
58 ········List<ClassNode> persistentClasses = new List<ClassNode>();
59 ········public List<ClassNode> PersistentClasses
60 ········{
61 ············get { return persistentClasses; }
62 ········}
63
64 ········string shortName;
65 ········public string ShortName
66 ········{
67 ············get { return shortName; }
68 ········}
69
70 ········string fullName;
71 ········public string FullName
72 ········{
73 ············get { return fullName; }
74 ········}
75
76 ········public string TargetFramework { get; set; }
77
78
79 ········List<string> analyzedTypes = new List<string>();
80
81 ········private void AnalyzeType(Type t)
82 ········{
83 ············if (t.IsGenericType && !t.IsGenericTypeDefinition)
84 ················return;
85 ············string tname = t.FullName;
86 ············if (analyzedTypes.Contains(tname))
87 ················return;
88 ············analyzedTypes.Add(tname);
89 ············Type ifc = t.GetInterface("NDO.IPersistenceCapable");
90 ············if (t.IsPublic && t.IsInterface && IsPersistentType(t))
91 ············{
92 ················this.persistentClasses.Add(new ClassNode(t, mappings));
93 ············}············
94 ············if ((ifc != null || IsPersistentType(t))
95 ················&& t.IsClass
96 ················&& (t.IsPublic || (t.MemberType & MemberTypes.NestedType) != 0))
97 ············{
98 ················this.persistentClasses.Add(new ClassNode(t, mappings));
99 ················if (t.BaseType.FullName != "System.Object")
100 ····················AnalyzeType(t.BaseType);
101 ················this.AnalyzeTypes(t.GetNestedTypes());
102 ············}
103 ········}
104
105 ········private bool IsPersistentType(Type t)
106 ········{
107 return t. GetCustomAttributes( false ) . Any( ca => ca. GetType( ) . Name == "NDOPersistentAttribute" ) ;
 
108 ········}
109
110
111 ········private void AnalyzeTypes(Type[] types)
112 ········{
113 ············foreach (Type t in types)
114 ············{
115 ················AnalyzeType(t);
116 ············}
117 ········}
118
119 ········public AssemblyNode(Assembly ass, NDOMapping mappings)
120 ········{
121 ············this.mappings = mappings;
122 ············this.shortName = ass.FullName.Substring(0, ass.FullName.IndexOf(','));
123 ············string dllName = ass.Location;
124
125 ············try
126 ············{
127 ················Type[] theTypes = ass.GetTypes();
128 ············}
129 ············catch (ReflectionTypeLoadException tlex)
130 ············{
131 ················string s = string.Empty;
132 ················foreach (Exception ex in tlex.LoaderExceptions)
133 ················{
134 ····················s += ex.Message + "\r\n";
135 ················}
136 ················throw new Exception("AssemblyNode: ReflectionTypeLoadException occured: " + s);
137 ············}
138 ············AnalyzeTypes(ass.GetTypes());
139 ············this.fullName = ass.FullName;
140 ············object[] attrs = ass.GetCustomAttributes(false);
141 ············this.isEnhanced = (HasEnhancedAttribute(attrs));
142 ············this.oidTypeName = GetOidTypeNameFromAttributes(attrs);
143 ············DetermineTargetFramework( attrs );
144 ········}
145
146 ········private string GetOidTypeNameFromAttributes(object[] attributes)
147 ········{
148 ············foreach (System.Attribute attr in attributes)
149 ············{
150 ················OidColumnAttribute oidAttr = attr as OidColumnAttribute;
151 ················if (oidAttr != null && oidAttr.NetType != null)
152 ················{
153 ····················this.oidType = oidAttr.NetType;
154 ····················return oidType.FullName;
155 ················}
156 ············}
157
158 ············return null;
159 ········}
160
161 ········private bool HasEnhancedAttribute(object[] attributes)
162 ········{
163 ············foreach (System.Attribute attr in attributes)
164 ············{
165 ················if (attr is NDOEnhancedAttribute)
166 ················{
167 ····················return true;
168 ················}
169 ············}············
170 ············return false;
171 ········}
172
173 ········private void DetermineTargetFramework( object[] attributes )
174 ········{
175 ············foreach (System.Attribute attr in attributes)
176 ············{
177 ················if (attr.GetType().Name == "TargetFrameworkAttribute")
178 ················{
179 ····················this.TargetFramework = ((TargetFrameworkAttribute)attr).FrameworkName;
180 ····················return;
181 ················}
182 ············}
183 ············this.TargetFramework = "Unknown";
184 ········}
185
186 ····}
187 }
188
New Commit (af20317)
1 //
2 // Copyright (c) 2002-2022 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Linq;
25 using System.Collections.Generic;
26 using System.Reflection;
27 using NDO;
28 using NDO.Mapping;
29 using System.Runtime.Versioning;
30 using NDO.Mapping.Attributes;
31
32 namespace NDOEnhancer
33 {
34 ····/// <summary>
35 ····/// Represents some properties of assemblies.
36 ····/// </summary>
37 ····internal class AssemblyNode
38 ····{
39
40 ········NDOMapping mappings;
41 ········
42 ········bool isEnhanced;
43 ········public bool IsEnhanced
44 ········{
45 ············get { return isEnhanced; }
46 ········}
47 ········string oidTypeName;
48 ········public string OidTypeName
49 ········{
50 ············get { return oidTypeName; }
51 ········}
52 ········Type oidType;
53 ········public Type OidType
54 ········{
55 ············get { return oidType; }
56 ········}
57
58 ········List<ClassNode> persistentClasses = new List<ClassNode>();
59 ········public List<ClassNode> PersistentClasses
60 ········{
61 ············get { return persistentClasses; }
62 ········}
63
64 ········string shortName;
65 ········public string ShortName
66 ········{
67 ············get { return shortName; }
68 ········}
69
70 ········string fullName;
71 ········public string FullName
72 ········{
73 ············get { return fullName; }
74 ········}
75
76 ········public string TargetFramework { get; set; }
77
78
79 ········List<string> analyzedTypes = new List<string>();
80
81 ········private void AnalyzeType(Type t)
82 ········{
83 ············if (t.IsGenericType && !t.IsGenericTypeDefinition)
84 ················return;
85 ············string tname = t.FullName;
86 ············if (analyzedTypes.Contains(tname))
87 ················return;
88 ············analyzedTypes.Add(tname);
89 ············Type ifc = t.GetInterface("NDO.IPersistenceCapable");
90 ············if (t.IsPublic && t.IsInterface && IsPersistentType(t))
91 ············{
92 ················this.persistentClasses.Add(new ClassNode(t, mappings));
93 ············}············
94 ············if ((ifc != null || IsPersistentType(t))
95 ················&& t.IsClass
96 ················&& (t.IsPublic || (t.MemberType & MemberTypes.NestedType) != 0))
97 ············{
98 ················this.persistentClasses.Add(new ClassNode(t, mappings));
99 ················if (t.BaseType.FullName != "System.Object")
100 ····················AnalyzeType(t.BaseType);
101 ················this.AnalyzeTypes(t.GetNestedTypes());
102 ············}
103 ········}
104
105 ········private bool IsPersistentType(Type t)
106 ········{
107 return t. GetCustomAttribute( typeof( NDOPersistentAttribute ) ) != null;
108 ············//return t.GetCustomAttributes( false ).Any( ca => ca.GetType().Name == "NDOPersistentAttribute" );
109 ········}
110
111
112 ········private void AnalyzeTypes(Type[] types)
113 ········{
114 ············foreach (Type t in types)
115 ············{
116 ················AnalyzeType(t);
117 ············}
118 ········}
119
120 ········public AssemblyNode(Assembly ass, NDOMapping mappings)
121 ········{
122 ············this.mappings = mappings;
123 ············this.shortName = ass.FullName.Substring(0, ass.FullName.IndexOf(','));
124 ············string dllName = ass.Location;
125
126 ············try
127 ············{
128 ················Type[] theTypes = ass.GetTypes();
129 ············}
130 ············catch (ReflectionTypeLoadException tlex)
131 ············{
132 ················string s = string.Empty;
133 ················foreach (Exception ex in tlex.LoaderExceptions)
134 ················{
135 ····················s += ex.Message + "\r\n";
136 ················}
137 ················throw new Exception("AssemblyNode: ReflectionTypeLoadException occured: " + s);
138 ············}
139 ············AnalyzeTypes(ass.GetTypes());
140 ············this.fullName = ass.FullName;
141 ············object[] attrs = ass.GetCustomAttributes(false);
142 ············this.isEnhanced = (HasEnhancedAttribute(attrs));
143 ············this.oidTypeName = GetOidTypeNameFromAttributes(attrs);
144 ············DetermineTargetFramework( attrs );
145 ········}
146
147 ········private string GetOidTypeNameFromAttributes(object[] attributes)
148 ········{
149 ············foreach (System.Attribute attr in attributes)
150 ············{
151 ················OidColumnAttribute oidAttr = attr as OidColumnAttribute;
152 ················if (oidAttr != null && oidAttr.NetType != null)
153 ················{
154 ····················this.oidType = oidAttr.NetType;
155 ····················return oidType.FullName;
156 ················}
157 ············}
158
159 ············return null;
160 ········}
161
162 ········private bool HasEnhancedAttribute(object[] attributes)
163 ········{
164 ············foreach (System.Attribute attr in attributes)
165 ············{
166 ················if (attr is NDOEnhancedAttribute)
167 ················{
168 ····················return true;
169 ················}
170 ············}············
171 ············return false;
172 ········}
173
174 ········private void DetermineTargetFramework( object[] attributes )
175 ········{
176 ············foreach (System.Attribute attr in attributes)
177 ············{
178 ················if (attr.GetType().Name == "TargetFrameworkAttribute")
179 ················{
180 ····················this.TargetFramework = ((TargetFrameworkAttribute)attr).FrameworkName;
181 ····················return;
182 ················}
183 ············}
184 ············this.TargetFramework = "Unknown";
185 ········}
186
187 ····}
188 }
189