Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeName.cs @ 7259

Last change on this file since 7259 was 7259, checked in by swagner, 11 years ago

Updated year of copyrights to 2012 (#1716)

File size: 10.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Persistence.Auxiliary {
29
30  /// <summary>
31  /// Contains a more modular representation of type names that can
32  /// be used to compare versions and ignore extended assembly
33  /// attributes.
34  /// </summary>
35  [StorableClass]
36  public class TypeName {
37
38    /// <summary>
39    /// Gets or sets the namespace.
40    /// </summary>
41    /// <value>The namespace.</value>
42    [Storable]
43    public string Namespace { get; private set; }
44
45    /// <summary>
46    /// Gets or sets the name of the class.
47    /// </summary>
48    /// <value>The name of the class.</value>
49    [Storable]
50    public string ClassName { get; private set; }
51
52    /// <summary>
53    /// Gets or sets the generic args.
54    /// </summary>
55    /// <value>The generic args.</value>
56    [Storable]
57    public List<TypeName> GenericArgs { get; internal set; }
58
59    /// <summary>
60    /// Gets a value indicating whether this instance is generic.
61    /// </summary>
62    /// <value>
63    ///   <c>true</c> if this instance is generic; otherwise, <c>false</c>.
64    /// </value>
65    public bool IsGeneric { get { return GenericArgs.Count > 0; } }
66
67    /// <summary>
68    /// Gets or sets the memory magic (point or array declaration).
69    /// </summary>
70    /// <value>The memory magic.</value>
71    [Storable]
72    public string MemoryMagic { get; internal set; }
73
74    /// <summary>
75    /// Gets or sets the name of the assembly.
76    /// </summary>
77    /// <value>The name of the assembly.</value>
78    [Storable]
79    public string AssemblyName { get; internal set; }
80
81    /// <summary>
82    /// Gets or sets the assembly attribues.
83    /// </summary>
84    /// <value>The assembly attribues.</value>
85    [Storable]
86    public Dictionary<string, string> AssemblyAttribues { get; internal set; }
87
88    /// <summary>
89    /// Gets or sets a value indicating whether this instance is reference.
90    /// </summary>
91    /// <value>
92    ///   <c>true</c> if this instance is reference; otherwise, <c>false</c>.
93    /// </value>
94    [Storable]
95    public bool IsReference { get; internal set; }
96
97
98    [StorableConstructor]
99    protected TypeName(bool deserializing) { }
100    /// <summary>
101    /// Initializes a new instance of the <see cref="TypeName"/> class.
102    /// </summary>
103    /// <param name="nameSpace">The namespace.</param>
104    /// <param name="className">Name of the class.</param>
105    internal TypeName(string nameSpace, string className) {
106      Namespace = nameSpace;
107      ClassName = className;
108      GenericArgs = new List<TypeName>();
109      MemoryMagic = "";
110      AssemblyAttribues = new Dictionary<string, string>();
111    }
112
113
114    /// <summary>
115    /// Returns a <see cref="System.String"/> that represents this instance.
116    /// </summary>
117    /// <param name="full">if set to <c>true</c> includes full information
118    /// about generic parameters and assembly properties.</param>
119    /// <returns>
120    /// A <see cref="System.String"/> that represents this instance.
121    /// </returns>
122    public string ToString(bool full) {
123      return ToString(full, true);
124    }
125
126
127    /// <summary>
128    /// Returns a <see cref="System.String"/> that represents this instance.
129    /// </summary>
130    /// <param name="full">if set to <c>true</c> includes full information
131    /// about generic parameters and assembly properties.</param>
132    /// <param name="includeAssembly">if set to <c>true</c> include assembly properties and generic parameters.</param>
133    /// <returns>
134    /// A <see cref="System.String"/> that represents this instance.
135    /// </returns>
136    public string ToString(bool full, bool includeAssembly) {
137      StringBuilder sb = new StringBuilder();
138      if (!string.IsNullOrEmpty(Namespace))
139        sb.Append(Namespace).Append('.');
140      sb.Append(ClassName);
141      if (IsGeneric) {
142        sb.Append('`').Append(GenericArgs.Count).Append('[');
143        bool first = true;
144        foreach (TypeName t in GenericArgs) {
145          if (first)
146            first = false;
147          else
148            sb.Append(',');
149          sb.Append('[').Append(t.ToString(full)).Append(']');
150        }
151        sb.Append(']');
152      }
153      sb.Append(MemoryMagic);
154      if (includeAssembly && AssemblyName != null) {
155        sb.Append(", ").Append(AssemblyName);
156        if (full)
157          foreach (var property in AssemblyAttribues)
158            sb.Append(", ").Append(property.Key).Append('=').Append(property.Value);
159      }
160      return sb.ToString();
161    }
162
163    public string GetTypeNameInCode(HashSet<string> omitNamespaces) {
164      StringBuilder sb = new StringBuilder();
165      if (!string.IsNullOrEmpty(Namespace) && omitNamespaces == null || !omitNamespaces.Contains(Namespace))
166        sb.Append(Namespace).Append('.');
167      sb.Append(ClassName);
168      if (IsGeneric) {
169        sb.Append("<");
170        sb.Append(
171          string.Join(", ",
172            GenericArgs
173              .Select(a => a.GetTypeNameInCode(omitNamespaces))
174              .ToArray()));
175        sb.Append(">");
176      }
177      sb.Append(MemoryMagic);
178      return sb.ToString();
179    }
180
181    public string GetTypeNameInCode(bool includeAllNamespaces) {
182      StringBuilder sb = new StringBuilder();
183      if (includeAllNamespaces)
184        sb.Append(Namespace).Append('.');
185      sb.Append(ClassName);
186      if (IsGeneric) {
187        sb.Append("<");
188        sb.Append(
189          string.Join(", ",
190            GenericArgs
191              .Select(a => a.GetTypeNameInCode(includeAllNamespaces))
192              .ToArray()));
193        sb.Append(">");
194      }
195      sb.Append(MemoryMagic);
196      return sb.ToString();
197    }
198
199
200    /// <summary>
201    /// Returns a <see cref="System.String"/> that represents this instance.
202    /// </summary>
203    /// <returns>
204    /// A <see cref="System.String"/> that represents this instance.
205    /// </returns>
206    public override string ToString() {
207      return ToString(true);
208    }
209
210
211    /// <summary>
212    /// Lexicographically compare version information and make sure type and assembly
213    /// names are identical. This function recursively checks generic type arguments.
214    /// </summary>
215    /// <param name="typeName">Name of the type.</param>
216    /// <returns>
217    ///   <c>true</c> if is newer than the specified type name; otherwise, <c>false</c>.
218    /// </returns>
219    public bool IsNewerThan(TypeName typeName) {
220      try {
221        if (this.ClassName != typeName.ClassName ||
222          this.Namespace != typeName.Namespace ||
223          this.AssemblyName != typeName.AssemblyName)
224          throw new Exception("Cannot compare versions of different types");
225        if (CompareVersions(
226          this.AssemblyAttribues["Version"],
227          typeName.AssemblyAttribues["Version"]) > 0)
228          return true;
229        IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
230        IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
231        while (thisIt.MoveNext()) {
232          tIt.MoveNext();
233          if (thisIt.Current.IsNewerThan(tIt.Current))
234            return true;
235        }
236        return false;
237      } catch (KeyNotFoundException) {
238        throw new Exception("Could not extract version information from type string");
239      }
240    }
241
242
243    /// <summary>
244    /// Make sure major and minor version number are identical. This function
245    /// recursively checks generic type arguments.
246    /// </summary>
247    /// <param name="typeName">Name of the type.</param>
248    /// <returns>
249    ///   <c>true</c> if the specified type names are compatible; otherwise, <c>false</c>.
250    /// </returns>
251    public bool IsCompatible(TypeName typeName) {
252      try {
253        if (this.ClassName != typeName.ClassName ||
254          this.Namespace != typeName.Namespace ||
255          this.AssemblyName != typeName.AssemblyName)
256          throw new Exception("Cannot compare versions of different types");
257        Version thisVersion = new Version(this.AssemblyAttribues["Version"]);
258        Version tVersion = new Version(typeName.AssemblyAttribues["Version"]);
259        if (this.AssemblyName == "mscorlib" &&
260          (thisVersion.Major == 2 || thisVersion.Major == 4) &&
261          (tVersion.Major == 2 || tVersion.Major == 4)) {
262          // skip version check
263        } else if (thisVersion.Major != tVersion.Major ||
264                   thisVersion.Minor != tVersion.Minor)
265          return false;
266        IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
267        IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
268        while (thisIt.MoveNext()) {
269          tIt.MoveNext();
270          if (!thisIt.Current.IsCompatible(tIt.Current))
271            return false;
272        }
273        return true;
274      } catch (KeyNotFoundException) {
275        throw new Exception("Could not extract version infomration from type string");
276      }
277    }
278
279    private static int CompareVersions(string v1string, string v2string) {
280      return new Version(v1string).CompareTo(new Version(v2string));
281    }
282  }
283}
Note: See TracBrowser for help on using the repository browser.