1 | using System;
|
---|
2 | using System.Text;
|
---|
3 | using System.Text.RegularExpressions;
|
---|
4 | using System.Reflection.Emit;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Persistence.Auxiliary {
|
---|
9 |
|
---|
10 | /// <summary>
|
---|
11 | /// Contains a more modular representation of type names that can
|
---|
12 | /// be used to compare versions and ignore extended assembly
|
---|
13 | /// attributes.
|
---|
14 | /// </summary>
|
---|
15 | [StorableClass]
|
---|
16 | public class TypeName {
|
---|
17 |
|
---|
18 | /// <summary>
|
---|
19 | /// Gets or sets the namespace.
|
---|
20 | /// </summary>
|
---|
21 | /// <value>The namespace.</value>
|
---|
22 | [Storable]
|
---|
23 | public string Namespace { get; private set; }
|
---|
24 |
|
---|
25 | /// <summary>
|
---|
26 | /// Gets or sets the name of the class.
|
---|
27 | /// </summary>
|
---|
28 | /// <value>The name of the class.</value>
|
---|
29 | [Storable]
|
---|
30 | public string ClassName { get; private set; }
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// Gets or sets the generic args.
|
---|
34 | /// </summary>
|
---|
35 | /// <value>The generic args.</value>
|
---|
36 | [Storable]
|
---|
37 | public List<TypeName> GenericArgs { get; internal set; }
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Gets a value indicating whether this instance is generic.
|
---|
41 | /// </summary>
|
---|
42 | /// <value>
|
---|
43 | /// <c>true</c> if this instance is generic; otherwise, <c>false</c>.
|
---|
44 | /// </value>
|
---|
45 | public bool IsGeneric { get { return GenericArgs.Count > 0; } }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Gets or sets the memory magic (point or array declaration).
|
---|
49 | /// </summary>
|
---|
50 | /// <value>The memory magic.</value>
|
---|
51 | [Storable]
|
---|
52 | public string MemoryMagic { get; internal set; }
|
---|
53 |
|
---|
54 | /// <summary>
|
---|
55 | /// Gets or sets the name of the assembly.
|
---|
56 | /// </summary>
|
---|
57 | /// <value>The name of the assembly.</value>
|
---|
58 | [Storable]
|
---|
59 | public string AssemblyName { get; internal set; }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Gets or sets the assembly attribues.
|
---|
63 | /// </summary>
|
---|
64 | /// <value>The assembly attribues.</value>
|
---|
65 | [Storable]
|
---|
66 | public Dictionary<string, string> AssemblyAttribues { get; internal set; }
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Gets or sets a value indicating whether this instance is reference.
|
---|
70 | /// </summary>
|
---|
71 | /// <value>
|
---|
72 | /// <c>true</c> if this instance is reference; otherwise, <c>false</c>.
|
---|
73 | /// </value>
|
---|
74 | [Storable]
|
---|
75 | public bool IsReference { get; internal set; }
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Initializes a new instance of the <see cref="TypeName"/> class.
|
---|
81 | /// </summary>
|
---|
82 | /// <param name="nameSpace">The namespace.</param>
|
---|
83 | /// <param name="className">Name of the class.</param>
|
---|
84 | internal TypeName(string nameSpace, string className) {
|
---|
85 | Namespace = nameSpace;
|
---|
86 | ClassName = className;
|
---|
87 | GenericArgs = new List<TypeName>();
|
---|
88 | MemoryMagic = "";
|
---|
89 | AssemblyAttribues = new Dictionary<string, string>();
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /// <summary>
|
---|
94 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
95 | /// </summary>
|
---|
96 | /// <param name="full">if set to <c>true</c> includes full information
|
---|
97 | /// about generic parameters and assembly properties.</param>
|
---|
98 | /// <returns>
|
---|
99 | /// A <see cref="System.String"/> that represents this instance.
|
---|
100 | /// </returns>
|
---|
101 | public string ToString(bool full) {
|
---|
102 | return ToString(full, true);
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /// <summary>
|
---|
107 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
108 | /// </summary>
|
---|
109 | /// <param name="full">if set to <c>true</c> includes full information
|
---|
110 | /// about generic parameters and assembly properties.</param>
|
---|
111 | /// <param name="includeAssembly">if set to <c>true</c> include assembly properties and generic parameters.</param>
|
---|
112 | /// <returns>
|
---|
113 | /// A <see cref="System.String"/> that represents this instance.
|
---|
114 | /// </returns>
|
---|
115 | public string ToString(bool full, bool includeAssembly) {
|
---|
116 | StringBuilder sb = new StringBuilder();
|
---|
117 | if (!string.IsNullOrEmpty(Namespace))
|
---|
118 | sb.Append(Namespace).Append('.');
|
---|
119 | sb.Append(ClassName);
|
---|
120 | if (IsGeneric) {
|
---|
121 | sb.Append('`').Append(GenericArgs.Count).Append('[');
|
---|
122 | bool first = true;
|
---|
123 | foreach (TypeName t in GenericArgs) {
|
---|
124 | if (first)
|
---|
125 | first = false;
|
---|
126 | else
|
---|
127 | sb.Append(',');
|
---|
128 | sb.Append('[').Append(t.ToString(full)).Append(']');
|
---|
129 | }
|
---|
130 | sb.Append(']');
|
---|
131 | }
|
---|
132 | sb.Append(MemoryMagic);
|
---|
133 | if (includeAssembly && AssemblyName != null) {
|
---|
134 | sb.Append(", ").Append(AssemblyName);
|
---|
135 | if (full)
|
---|
136 | foreach (var property in AssemblyAttribues)
|
---|
137 | sb.Append(", ").Append(property.Key).Append('=').Append(property.Value);
|
---|
138 | }
|
---|
139 | return sb.ToString();
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /// <summary>
|
---|
144 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
145 | /// </summary>
|
---|
146 | /// <returns>
|
---|
147 | /// A <see cref="System.String"/> that represents this instance.
|
---|
148 | /// </returns>
|
---|
149 | public override string ToString() {
|
---|
150 | return ToString(true);
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /// <summary>
|
---|
155 | /// Lexicographically compare version information and make sure type and assembly
|
---|
156 | /// names are identical. This function recursively checks generic type arguments.
|
---|
157 | /// </summary>
|
---|
158 | /// <param name="typeName">Name of the type.</param>
|
---|
159 | /// <returns>
|
---|
160 | /// <c>true</c> if is newer than the specified type name; otherwise, <c>false</c>.
|
---|
161 | /// </returns>
|
---|
162 | public bool IsNewerThan(TypeName typeName) {
|
---|
163 | try {
|
---|
164 | if (this.ClassName != typeName.ClassName ||
|
---|
165 | this.Namespace != typeName.Namespace ||
|
---|
166 | this.AssemblyName != typeName.AssemblyName)
|
---|
167 | throw new Exception("Cannot compare versions of different types");
|
---|
168 | if (CompareVersions(
|
---|
169 | this.AssemblyAttribues["Version"],
|
---|
170 | typeName.AssemblyAttribues["Version"]) > 0)
|
---|
171 | return true;
|
---|
172 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
173 | IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
|
---|
174 | while (thisIt.MoveNext()) {
|
---|
175 | tIt.MoveNext();
|
---|
176 | if (thisIt.Current.IsNewerThan(tIt.Current))
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 | return false;
|
---|
180 | } catch (KeyNotFoundException) {
|
---|
181 | throw new Exception("Could not extract version information from type string");
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /// <summary>
|
---|
187 | /// Make sure major and minor version number are identical. This function
|
---|
188 | /// recursively checks generic type arguments.
|
---|
189 | /// </summary>
|
---|
190 | /// <param name="typeName">Name of the type.</param>
|
---|
191 | /// <returns>
|
---|
192 | /// <c>true</c> if the specified type names are compatible; otherwise, <c>false</c>.
|
---|
193 | /// </returns>
|
---|
194 | public bool IsCompatible(TypeName typeName) {
|
---|
195 | try {
|
---|
196 | if (this.ClassName != typeName.ClassName ||
|
---|
197 | this.Namespace != typeName.Namespace ||
|
---|
198 | this.AssemblyName != typeName.AssemblyName)
|
---|
199 | throw new Exception("Cannot compare versions of different types");
|
---|
200 | Version thisVersion = new Version(this.AssemblyAttribues["Version"]);
|
---|
201 | Version tVersion = new Version(typeName.AssemblyAttribues["Version"]);
|
---|
202 | if (thisVersion.Major != tVersion.Major ||
|
---|
203 | thisVersion.Minor != tVersion.Minor)
|
---|
204 | return false;
|
---|
205 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
206 | IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
|
---|
207 | while (thisIt.MoveNext()) {
|
---|
208 | tIt.MoveNext();
|
---|
209 | if (!thisIt.Current.IsCompatible(tIt.Current))
|
---|
210 | return false;
|
---|
211 | }
|
---|
212 | return true;
|
---|
213 | } catch (KeyNotFoundException) {
|
---|
214 | throw new Exception("Could not extract version infomration from type string");
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | private static int CompareVersions(string v1string, string v2string) {
|
---|
219 | return new Version(v1string).CompareTo(new Version(v2string));
|
---|
220 | }
|
---|
221 | }
|
---|
222 | } |
---|