Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeName.cs @ 2396

Last change on this file since 2396 was 1795, checked in by epitzer, 15 years ago

Also make sure major and minor version match (not only newer) + better tests. (#613)

File size: 4.0 KB
Line 
1using System;
2using System.Text;
3using System.Text.RegularExpressions;
4using System.Reflection.Emit;
5using System.Collections.Generic;
6
7namespace HeuristicLab.Persistence.Auxiliary {
8 
9  public class TypeName {
10    public string Namespace { get; private set; }
11    public string ClassName { get; private set; }
12    public List<TypeName> GenericArgs { get; internal set; }
13    public bool IsGeneric { get { return GenericArgs.Count > 0; } }
14    public string MemoryMagic { get; internal set; }
15    public string AssemblyName { get; internal set; }
16    public Dictionary<string, string> AssemblyAttribues { get; internal set; }
17    public bool IsReference { get; internal set; }
18
19    internal TypeName(string nameSpace, string className) {
20      Namespace = nameSpace;
21      ClassName = className;
22      GenericArgs = new List<TypeName>();
23      MemoryMagic = "";
24      AssemblyAttribues = new Dictionary<string, string>();
25    }
26
27    public string ToString(bool full) {
28      return ToString(full, true);
29    }
30
31    public string ToString(bool full, bool includeAssembly) {
32      StringBuilder sb = new StringBuilder();
33      sb.Append(Namespace).Append('.').Append(ClassName);
34      if (IsGeneric) {
35        sb.Append('`').Append(GenericArgs.Count).Append('[');
36        bool first = true;
37        foreach (TypeName t in GenericArgs) {
38          if (first)
39            first = false;
40          else
41            sb.Append(',');
42          sb.Append('[').Append(t.ToString(full)).Append(']');
43        }
44        sb.Append(']');
45      }
46      sb.Append(MemoryMagic);
47      if (includeAssembly && AssemblyName != null) {
48        sb.Append(", ").Append(AssemblyName);
49        if (full)
50          foreach (var property in AssemblyAttribues)
51            sb.Append(", ").Append(property.Key).Append('=').Append(property.Value);
52      }
53      return sb.ToString();
54    }
55
56    public override string ToString() {
57      return ToString(true);
58    }
59
60    public bool IsNewerThan(TypeName t) {
61      try {
62        if (this.ClassName != t.ClassName ||
63          this.Namespace != t.Namespace ||
64          this.AssemblyName != t.AssemblyName)
65          throw new Exception("Cannot compare versions of different types");
66        if (CompareVersions(
67          this.AssemblyAttribues["Version"],
68          t.AssemblyAttribues["Version"]) > 0)
69          return true;
70        IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
71        IEnumerator<TypeName> tIt = t.GenericArgs.GetEnumerator();
72        while (thisIt.MoveNext()) {
73          tIt.MoveNext();
74          if (thisIt.Current.IsNewerThan(tIt.Current))
75            return true;
76        }
77        return false;
78      } catch (KeyNotFoundException) {
79        throw new Exception("Could not extract version information from type string");
80      }
81    }
82
83    public bool IsCompatible(TypeName t) {
84      try {
85        if (this.ClassName != t.ClassName ||
86          this.Namespace != t.Namespace ||
87          this.AssemblyName != t.AssemblyName)
88          throw new Exception("Cannot compare versions of different types");
89        Version thisVersion = new Version(this.AssemblyAttribues["Version"]);
90        Version tVersion = new Version(t.AssemblyAttribues["Version"]);
91        if (thisVersion.Major != tVersion.Major ||
92          thisVersion.Minor != tVersion.Minor)
93          return false;
94        IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
95        IEnumerator<TypeName> tIt = t.GenericArgs.GetEnumerator();
96        while (thisIt.MoveNext()) {
97          tIt.MoveNext();
98          if (!thisIt.Current.IsCompatible(tIt.Current))
99            return false;
100        }
101        return true;
102      } catch (KeyNotFoundException) {
103        throw new Exception("Could not extract version infomration from type string");
104      }
105    }
106
107    private static int CompareVersions(string v1string, string v2string) {
108      return new Version(v1string).CompareTo(new Version(v2string));
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.