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.
32 namespace System
.Runtime
.Versioning
35 public sealed class FrameworkName
: IEquatable
<FrameworkName
>
40 public string FullName
{
42 if (fullName
== null) {
43 var sb
= new StringBuilder (Identifier
);
44 sb
.Append (",Version=v");
45 sb
.Append (Version
.ToString ());
47 string profile
= Profile
;
48 if (!String
.IsNullOrEmpty (profile
)) {
49 sb
.Append (",Profile=");
53 fullName
= sb
.ToString ();
60 public string Identifier
{
64 public string Profile
{
68 public Version Version
{
72 public FrameworkName (string frameworkName
)
74 if (frameworkName
== null)
75 throw new ArgumentNullException ("frameworkName");
77 if (frameworkName
.Length
== 0)
78 throw new ArgumentException ("The parameter 'frameworkName' cannot be an empty string.", "frameworkName");
80 this.Profile
= String
.Empty
;
81 ParseFrameworkName (frameworkName
);
84 public FrameworkName (string identifier
, Version version
)
85 : this (identifier
, version
, String
.Empty
)
89 public FrameworkName (string identifier
, Version version
, string profile
)
91 if (identifier
== null)
92 throw new ArgumentNullException ("identifier");
95 throw new ArgumentNullException ("version");
97 if (identifier
.Length
== 0)
98 throw new ArgumentException ("The parameter 'identifier' cannot be an empty string.", "identifier");
100 this.Identifier
= identifier
;
101 this.Version
= version
;
103 this.Profile
= String
.Empty
;
105 this.Profile
= profile
;
108 public bool Equals (FrameworkName other
)
110 return (other
.Version
== this.Version
&&
111 String
.Compare (other
.Identifier
, this.Identifier
, StringComparison
.Ordinal
) == 0 &&
112 String
.Compare (other
.Profile
, this.Profile
, StringComparison
.Ordinal
) == 0);
115 public override int GetHashCode ()
117 if (hashCode
== null) {
118 hashCode
= Version
.GetHashCode () ^ Identifier
.GetHashCode ();
119 string profile
= Profile
;
121 hashCode ^
= profile
.GetHashCode ();
124 return (int)hashCode
;
127 public override string ToString ()
132 public static bool operator == (FrameworkName left
, FrameworkName right
)
134 if (((object)left
) == null && ((object)right
) == null)
137 if (((object)left
) == null || ((object)right
) == null)
140 return left
.Equals (right
);
143 public static bool operator != (FrameworkName left
, FrameworkName right
)
145 if (((object)left
) == null && ((object)right
) == null)
148 if (((object)left
) == null || ((object)right
) == null)
151 return !left
.Equals (right
);
154 void ParseFrameworkName (string frameworkName
)
156 string[] parts
= frameworkName
.Split (',');
157 int len
= parts
.Length
;
159 if (len
< 2 || len
> 3)
160 throw new ArgumentException ("FrameworkName cannot have less than two components or more than three components.");
162 bool invalid
= false;
167 for (int i
= 0; i
< len
; i
++) {
168 part
= parts
[i
].Trim ();
169 if (part
.Length
== 0) {
174 splitPart
= part
.Split ('=');
175 splen
= splitPart
.Length
;
177 if (String
.Compare ("version", splitPart
[0], StringComparison
.OrdinalIgnoreCase
) == 0) {
178 if (i
== 0 || splen
!= 2) {
184 char first
= splitPart
[1][0];
185 if (first
== 'v' || first
== 'V')
186 splitPart
[1] = splitPart
[1].Substring (1);
187 this.Version
= new Version (splitPart
[1]);
188 } catch (Exception ex
) {
189 throw new ArgumentException ("FrameworkName version component is invalid.", ex
);
195 if (String
.Compare ("profile", splitPart
[0], StringComparison
.OrdinalIgnoreCase
) == 0) {
202 Profile
= String
.Join ("=", splitPart
, 1, splen
- 1);
217 throw new ArgumentException ("FrameworkName is invalid.");
220 throw new ArgumentException ("FrameworkName version component is missing.");