[3743] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3743] | 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 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3903] | 24 | using System.Linq;
|
---|
[1703] | 25 | using System.Text;
|
---|
[3004] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1703] | 27 |
|
---|
| 28 | namespace HeuristicLab.Persistence.Auxiliary {
|
---|
[3016] | 29 |
|
---|
[3004] | 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>
|
---|
[3017] | 35 | [StorableClass]
|
---|
[1776] | 36 | public class TypeName {
|
---|
[3004] | 37 |
|
---|
[3016] | 38 | /// <summary>
|
---|
| 39 | /// Gets or sets the namespace.
|
---|
| 40 | /// </summary>
|
---|
| 41 | /// <value>The namespace.</value>
|
---|
[3004] | 42 | [Storable]
|
---|
[1779] | 43 | public string Namespace { get; private set; }
|
---|
[3004] | 44 |
|
---|
[3016] | 45 | /// <summary>
|
---|
| 46 | /// Gets or sets the name of the class.
|
---|
| 47 | /// </summary>
|
---|
| 48 | /// <value>The name of the class.</value>
|
---|
[3004] | 49 | [Storable]
|
---|
[1779] | 50 | public string ClassName { get; private set; }
|
---|
[3004] | 51 |
|
---|
[3016] | 52 | /// <summary>
|
---|
| 53 | /// Gets or sets the generic args.
|
---|
| 54 | /// </summary>
|
---|
| 55 | /// <value>The generic args.</value>
|
---|
[3004] | 56 | [Storable]
|
---|
[1779] | 57 | public List<TypeName> GenericArgs { get; internal set; }
|
---|
[3036] | 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>
|
---|
[1776] | 65 | public bool IsGeneric { get { return GenericArgs.Count > 0; } }
|
---|
[3004] | 66 |
|
---|
[3016] | 67 | /// <summary>
|
---|
| 68 | /// Gets or sets the memory magic (point or array declaration).
|
---|
| 69 | /// </summary>
|
---|
| 70 | /// <value>The memory magic.</value>
|
---|
[3004] | 71 | [Storable]
|
---|
[1779] | 72 | public string MemoryMagic { get; internal set; }
|
---|
[3004] | 73 |
|
---|
[3016] | 74 | /// <summary>
|
---|
| 75 | /// Gets or sets the name of the assembly.
|
---|
| 76 | /// </summary>
|
---|
| 77 | /// <value>The name of the assembly.</value>
|
---|
[3004] | 78 | [Storable]
|
---|
[1779] | 79 | public string AssemblyName { get; internal set; }
|
---|
[3004] | 80 |
|
---|
[3016] | 81 | /// <summary>
|
---|
| 82 | /// Gets or sets the assembly attribues.
|
---|
| 83 | /// </summary>
|
---|
| 84 | /// <value>The assembly attribues.</value>
|
---|
[3004] | 85 | [Storable]
|
---|
[1779] | 86 | public Dictionary<string, string> AssemblyAttribues { get; internal set; }
|
---|
[3004] | 87 |
|
---|
[3016] | 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>
|
---|
[3004] | 94 | [Storable]
|
---|
[1779] | 95 | public bool IsReference { get; internal set; }
|
---|
| 96 |
|
---|
[3016] | 97 |
|
---|
[4806] | 98 | [StorableConstructor]
|
---|
| 99 | protected TypeName(bool deserializing) { }
|
---|
[3016] | 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>
|
---|
[1779] | 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>();
|
---|
[1776] | 111 | }
|
---|
| 112 |
|
---|
[3016] | 113 |
|
---|
| 114 | /// <summary>
|
---|
| 115 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
| 116 | /// </summary>
|
---|
[3036] | 117 | /// <param name="full">if set to <c>true</c> includes full information
|
---|
[3016] | 118 | /// about generic parameters and assembly properties.</param>
|
---|
| 119 | /// <returns>
|
---|
| 120 | /// A <see cref="System.String"/> that represents this instance.
|
---|
| 121 | /// </returns>
|
---|
[1779] | 122 | public string ToString(bool full) {
|
---|
[1780] | 123 | return ToString(full, true);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[3004] | 126 |
|
---|
[3016] | 127 | /// <summary>
|
---|
| 128 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
| 129 | /// </summary>
|
---|
[3036] | 130 | /// <param name="full">if set to <c>true</c> includes full information
|
---|
| 131 | /// about generic parameters and assembly properties.</param>
|
---|
[3016] | 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>
|
---|
[4068] | 136 | public string ToString(bool full, bool includeAssembly) {
|
---|
[1779] | 137 | StringBuilder sb = new StringBuilder();
|
---|
[2859] | 138 | if (!string.IsNullOrEmpty(Namespace))
|
---|
| 139 | sb.Append(Namespace).Append('.');
|
---|
| 140 | sb.Append(ClassName);
|
---|
[1779] | 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(']');
|
---|
[1703] | 152 | }
|
---|
[1779] | 153 | sb.Append(MemoryMagic);
|
---|
[1780] | 154 | if (includeAssembly && AssemblyName != null) {
|
---|
[1779] | 155 | sb.Append(", ").Append(AssemblyName);
|
---|
[1780] | 156 | if (full)
|
---|
| 157 | foreach (var property in AssemblyAttribues)
|
---|
| 158 | sb.Append(", ").Append(property.Key).Append('=').Append(property.Value);
|
---|
| 159 | }
|
---|
[1779] | 160 | return sb.ToString();
|
---|
[1703] | 161 | }
|
---|
| 162 |
|
---|
[3903] | 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 | }
|
---|
[3935] | 177 | sb.Append(MemoryMagic);
|
---|
[3903] | 178 | return sb.ToString();
|
---|
| 179 | }
|
---|
[3016] | 180 |
|
---|
[3935] | 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 | }
|
---|
[3903] | 198 |
|
---|
[3935] | 199 |
|
---|
[3016] | 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>
|
---|
[1779] | 206 | public override string ToString() {
|
---|
| 207 | return ToString(true);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[3004] | 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.
|
---|
[3016] | 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>
|
---|
[3004] | 219 | public bool IsNewerThan(TypeName typeName) {
|
---|
[1780] | 220 | try {
|
---|
[3004] | 221 | if (this.ClassName != typeName.ClassName ||
|
---|
| 222 | this.Namespace != typeName.Namespace ||
|
---|
| 223 | this.AssemblyName != typeName.AssemblyName)
|
---|
[1780] | 224 | throw new Exception("Cannot compare versions of different types");
|
---|
| 225 | if (CompareVersions(
|
---|
| 226 | this.AssemblyAttribues["Version"],
|
---|
[3004] | 227 | typeName.AssemblyAttribues["Version"]) > 0)
|
---|
[1779] | 228 | return true;
|
---|
[1780] | 229 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
[3004] | 230 | IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
|
---|
[1780] | 231 | while (thisIt.MoveNext()) {
|
---|
| 232 | tIt.MoveNext();
|
---|
[1795] | 233 | if (thisIt.Current.IsNewerThan(tIt.Current))
|
---|
[1780] | 234 | return true;
|
---|
| 235 | }
|
---|
| 236 | return false;
|
---|
[5172] | 237 | } catch (KeyNotFoundException) {
|
---|
[1780] | 238 | throw new Exception("Could not extract version information from type string");
|
---|
[1703] | 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[3004] | 242 |
|
---|
| 243 | /// <summary>
|
---|
| 244 | /// Make sure major and minor version number are identical. This function
|
---|
| 245 | /// recursively checks generic type arguments.
|
---|
| 246 | /// </summary>
|
---|
[3016] | 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>
|
---|
[3004] | 251 | public bool IsCompatible(TypeName typeName) {
|
---|
[1795] | 252 | try {
|
---|
[3004] | 253 | if (this.ClassName != typeName.ClassName ||
|
---|
| 254 | this.Namespace != typeName.Namespace ||
|
---|
| 255 | this.AssemblyName != typeName.AssemblyName)
|
---|
[1795] | 256 | throw new Exception("Cannot compare versions of different types");
|
---|
| 257 | Version thisVersion = new Version(this.AssemblyAttribues["Version"]);
|
---|
[3004] | 258 | Version tVersion = new Version(typeName.AssemblyAttribues["Version"]);
|
---|
[5172] | 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)
|
---|
[1795] | 265 | return false;
|
---|
| 266 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
[3004] | 267 | IEnumerator<TypeName> tIt = typeName.GenericArgs.GetEnumerator();
|
---|
[1795] | 268 | while (thisIt.MoveNext()) {
|
---|
| 269 | tIt.MoveNext();
|
---|
| 270 | if (!thisIt.Current.IsCompatible(tIt.Current))
|
---|
| 271 | return false;
|
---|
| 272 | }
|
---|
| 273 | return true;
|
---|
[5172] | 274 | } catch (KeyNotFoundException) {
|
---|
[1795] | 275 | throw new Exception("Could not extract version infomration from type string");
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[1779] | 279 | private static int CompareVersions(string v1string, string v2string) {
|
---|
| 280 | return new Version(v1string).CompareTo(new Version(v2string));
|
---|
[1776] | 281 | }
|
---|
[1703] | 282 | }
|
---|
| 283 | } |
---|