1 | using System;
|
---|
2 | using System.Text;
|
---|
3 | using System.Text.RegularExpressions;
|
---|
4 | using System.Reflection.Emit;
|
---|
5 | using System.Collections.Generic;
|
---|
6 |
|
---|
7 | namespace 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 | if (!string.IsNullOrEmpty(Namespace))
|
---|
34 | sb.Append(Namespace).Append('.');
|
---|
35 | sb.Append(ClassName);
|
---|
36 | if (IsGeneric) {
|
---|
37 | sb.Append('`').Append(GenericArgs.Count).Append('[');
|
---|
38 | bool first = true;
|
---|
39 | foreach (TypeName t in GenericArgs) {
|
---|
40 | if (first)
|
---|
41 | first = false;
|
---|
42 | else
|
---|
43 | sb.Append(',');
|
---|
44 | sb.Append('[').Append(t.ToString(full)).Append(']');
|
---|
45 | }
|
---|
46 | sb.Append(']');
|
---|
47 | }
|
---|
48 | sb.Append(MemoryMagic);
|
---|
49 | if (includeAssembly && AssemblyName != null) {
|
---|
50 | sb.Append(", ").Append(AssemblyName);
|
---|
51 | if (full)
|
---|
52 | foreach (var property in AssemblyAttribues)
|
---|
53 | sb.Append(", ").Append(property.Key).Append('=').Append(property.Value);
|
---|
54 | }
|
---|
55 | return sb.ToString();
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override string ToString() {
|
---|
59 | return ToString(true);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public bool IsNewerThan(TypeName t) {
|
---|
63 | try {
|
---|
64 | if (this.ClassName != t.ClassName ||
|
---|
65 | this.Namespace != t.Namespace ||
|
---|
66 | this.AssemblyName != t.AssemblyName)
|
---|
67 | throw new Exception("Cannot compare versions of different types");
|
---|
68 | if (CompareVersions(
|
---|
69 | this.AssemblyAttribues["Version"],
|
---|
70 | t.AssemblyAttribues["Version"]) > 0)
|
---|
71 | return true;
|
---|
72 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
73 | IEnumerator<TypeName> tIt = t.GenericArgs.GetEnumerator();
|
---|
74 | while (thisIt.MoveNext()) {
|
---|
75 | tIt.MoveNext();
|
---|
76 | if (thisIt.Current.IsNewerThan(tIt.Current))
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | return false;
|
---|
80 | } catch (KeyNotFoundException) {
|
---|
81 | throw new Exception("Could not extract version information from type string");
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public bool IsCompatible(TypeName t) {
|
---|
86 | try {
|
---|
87 | if (this.ClassName != t.ClassName ||
|
---|
88 | this.Namespace != t.Namespace ||
|
---|
89 | this.AssemblyName != t.AssemblyName)
|
---|
90 | throw new Exception("Cannot compare versions of different types");
|
---|
91 | Version thisVersion = new Version(this.AssemblyAttribues["Version"]);
|
---|
92 | Version tVersion = new Version(t.AssemblyAttribues["Version"]);
|
---|
93 | if (thisVersion.Major != tVersion.Major ||
|
---|
94 | thisVersion.Minor != tVersion.Minor)
|
---|
95 | return false;
|
---|
96 | IEnumerator<TypeName> thisIt = this.GenericArgs.GetEnumerator();
|
---|
97 | IEnumerator<TypeName> tIt = t.GenericArgs.GetEnumerator();
|
---|
98 | while (thisIt.MoveNext()) {
|
---|
99 | tIt.MoveNext();
|
---|
100 | if (!thisIt.Current.IsCompatible(tIt.Current))
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 | return true;
|
---|
104 | } catch (KeyNotFoundException) {
|
---|
105 | throw new Exception("Could not extract version infomration from type string");
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | private static int CompareVersions(string v1string, string v2string) {
|
---|
110 | return new Version(v1string).CompareTo(new Version(v2string));
|
---|
111 | }
|
---|
112 | }
|
---|
113 | } |
---|