2 // System.Runtime.Versioning.FrameworkName class
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2009 Novell, Inc (http://novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Runtime.Versioning
34 public sealed class FrameworkName : IEquatable <FrameworkName>
39 public string FullName {
41 if (fullName == null) {
42 var sb = new StringBuilder (Identifier);
43 sb.Append (",Version=v");
44 sb.Append (Version.ToString ());
46 string profile = Profile;
47 if (!String.IsNullOrEmpty (profile)) {
48 sb.Append (",Profile=");
52 fullName = sb.ToString ();
59 public string Identifier {
63 public string Profile {
67 public Version Version {
71 public FrameworkName (string frameworkName)
73 if (frameworkName == null)
74 throw new ArgumentNullException ("frameworkName");
76 if (frameworkName.Length == 0)
77 throw new ArgumentException ("The parameter 'frameworkName' cannot be an empty string.", "frameworkName");
79 this.Profile = String.Empty;
80 ParseFrameworkName (frameworkName);
83 public FrameworkName (string identifier, Version version)
84 : this (identifier, version, String.Empty)
88 public FrameworkName (string identifier, Version version, string profile)
90 if (identifier == null)
91 throw new ArgumentNullException ("identifier");
94 throw new ArgumentNullException ("version");
96 if (identifier.Length == 0)
97 throw new ArgumentException ("The parameter 'identifier' cannot be an empty string.", "identifier");
99 this.Identifier = identifier;
100 this.Version = version;
102 this.Profile = String.Empty;
104 this.Profile = profile;
107 public bool Equals (FrameworkName other)
109 if (Object.ReferenceEquals (other, null))
112 return (other.Version == this.Version &&
113 String.Compare (other.Identifier, this.Identifier, StringComparison.Ordinal) == 0 &&
114 String.Compare (other.Profile, this.Profile, StringComparison.Ordinal) == 0);
117 public override bool Equals (object obj)
119 return Equals (obj as FrameworkName);
122 public override int GetHashCode ()
124 if (hashCode == null) {
125 hashCode = Version.GetHashCode () ^ Identifier.GetHashCode ();
126 string profile = Profile;
128 hashCode ^= profile.GetHashCode ();
131 return (int)hashCode;
134 public override string ToString ()
139 public static bool operator == (FrameworkName left, FrameworkName right)
141 if (((object)left) == null && ((object)right) == null)
144 if (((object)left) == null || ((object)right) == null)
147 return left.Equals (right);
150 public static bool operator != (FrameworkName left, FrameworkName right)
152 if (((object)left) == null && ((object)right) == null)
155 if (((object)left) == null || ((object)right) == null)
158 return !left.Equals (right);
161 void ParseFrameworkName (string frameworkName)
163 string[] parts = frameworkName.Split (',');
164 int len = parts.Length;
166 if (len < 2 || len > 3)
167 throw new ArgumentException ("FrameworkName cannot have less than two components or more than three components.");
169 bool invalid = false;
174 for (int i = 0; i < len; i++) {
175 part = parts [i].Trim ();
176 if (part.Length == 0) {
181 splitPart = part.Split ('=');
182 splen = splitPart.Length;
184 if (String.Compare ("version", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
185 if (i == 0 || splen != 2) {
191 char first = splitPart [1][0];
192 if (first == 'v' || first == 'V')
193 splitPart [1] = splitPart [1].Substring (1);
194 this.Version = new Version (splitPart [1]);
195 } catch (Exception ex) {
196 throw new ArgumentException ("FrameworkName version component is invalid.", ex);
202 if (String.Compare ("profile", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
209 Profile = String.Join ("=", splitPart, 1, splen - 1);
224 throw new ArgumentException ("FrameworkName is invalid.");
227 throw new ArgumentException ("FrameworkName version component is missing.");