main
cs 198 lines 7.77 KB
Raw
1 // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3 namespace WixToolset.Dtf.Resources
4 {
5 using System;
6 using System.Diagnostics.CodeAnalysis;
7 using System.Globalization;
8
9 /// <summary>
10 /// Represents either a standard integer resource type or a custom resource type name.
11 /// </summary>
12 public class ResourceType
13 {
14 // Silence warnings about doc-comments
15 #pragma warning disable 1591
16
17 public static ResourceType None { get { return "#0"; } }
18 public static ResourceType Cursor { get { return "#1"; } }
19 public static ResourceType Bitmap { get { return "#2"; } }
20 public static ResourceType Icon { get { return "#3"; } }
21 public static ResourceType Menu { get { return "#4"; } }
22 public static ResourceType Dialog { get { return "#5"; } }
23 public static ResourceType String { get { return "#6"; } }
24 public static ResourceType FontDir { get { return "#7"; } }
25 public static ResourceType Font { get { return "#8"; } }
26 public static ResourceType Accelerator { get { return "#9"; } }
27 public static ResourceType RCData { get { return "#10"; } }
28 public static ResourceType MessageTable { get { return "#11"; } }
29 public static ResourceType GroupCursor { get { return "#12"; } }
30 public static ResourceType GroupIcon { get { return "#14"; } }
31 public static ResourceType Version { get { return "#16"; } }
32 public static ResourceType DialogInclude { get { return "#17"; } }
33 public static ResourceType PlugPlay { get { return "#19"; } }
34 public static ResourceType Vxd { get { return "#20"; } }
35 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ani")]
36 public static ResourceType AniCursor { get { return "#21"; } }
37 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ani")]
38 public static ResourceType AniIcon { get { return "#22"; } }
39 public static ResourceType Html { get { return "#23"; } }
40 public static ResourceType Manifest { get { return "#24"; } }
41
42 #pragma warning restore 1591
43
44 private string resourceType;
45
46 /// <summary>
47 /// Creates a new resource type from a string resource name.
48 /// </summary>
49 /// <param name="resourceType">String resource name,
50 /// or an integer resource type prefixed by a #.</param>
51 public ResourceType(string resourceType)
52 {
53 if (string.IsNullOrEmpty(resourceType))
54 {
55 throw new ArgumentNullException("resourceType");
56 }
57
58 this.resourceType = resourceType;
59
60 if (this.IsInteger && this.IntegerValue < 0)
61 {
62 throw new ArgumentOutOfRangeException("Invalid integer resource type value.");
63 }
64 }
65
66 /// <summary>
67 /// Creates a new integer resource type.
68 /// </summary>
69 /// <param name="resourceType">Integer value of a well-known resource type.</param>
70 public ResourceType(int resourceType)
71 : this("#" + resourceType)
72 {
73 }
74
75 /// <summary>
76 /// Gets a flag indicating whether the resource type is an integer type.
77 /// </summary>
78 public bool IsInteger
79 {
80 get
81 {
82 return this.resourceType.StartsWith("#", StringComparison.Ordinal);
83 }
84 }
85
86 /// <summary>
87 /// Gets the integer value of the resource type, or -1 if the resource type is not an integer.
88 /// </summary>
89 public int IntegerValue
90 {
91 get
92 {
93 int value;
94 if (!this.IsInteger ||
95 !Int32.TryParse(this.resourceType.Substring(1), out value))
96 {
97 value = -1;
98 }
99
100 return value;
101 }
102 }
103
104 /// <summary>
105 /// Gets a string representation of the resource type.
106 /// </summary>
107 /// <returns>The custom resource name, or the name of a well-known resource type.</returns>
108 public override string ToString()
109 {
110 if (this.IsInteger)
111 {
112 switch (this.IntegerValue)
113 {
114 case 0: return "None";
115 case 1: return "Cursor";
116 case 2: return "Bitmap";
117 case 3: return "Icon";
118 case 4: return "Menu";
119 case 5: return "Dialog";
120 case 6: return "String";
121 case 7: return "FontDir";
122 case 8: return "Font";
123 case 9: return "Accelerator";
124 case 10: return "RCData";
125 case 11: return "MessageTable";
126 case 12: return "GroupCursor";
127 case 14: return "GroupIcon";
128 case 16: return "Version";
129 case 17: return "DialogInclude";
130 case 19: return "PlugPlay";
131 case 20: return "Vxd";
132 case 21: return "AniCursor";
133 case 22: return "AniIcon";
134 case 23: return "Html";
135 case 24: return "Manifest";
136 }
137 }
138
139 return this.resourceType;
140 }
141
142 /// <summary>
143 /// Tests whether one resource type equals another object.
144 /// </summary>
145 /// <param name="obj">Other object.</param>
146 /// <returns>True if equal, else false.</returns>
147 public override bool Equals(object obj)
148 {
149 return this.Equals(obj as ResourceType);
150 }
151
152 /// <summary>
153 /// Tests whether one resource type equals another.
154 /// </summary>
155 /// <param name="otherType">Other resource type.</param>
156 /// <returns>True if equal, else false.</returns>
157 public bool Equals(ResourceType otherType)
158 {
159 return otherType != null && this.resourceType.Equals(otherType.resourceType, StringComparison.Ordinal);
160 }
161
162 /// <summary>
163 /// Gets a hash code suitable for using the resource type as a dictionary key.
164 /// </summary>
165 /// <returns>Hash code based on the resource type string.</returns>
166 public override int GetHashCode()
167 {
168 return this.resourceType.GetHashCode();
169 }
170
171 /// <summary>
172 /// Implicitly converts a string to a ResourceType.
173 /// </summary>
174 /// <param name="resourceType">String resource type to convert.</param>
175 /// <returns>ResourceType object.</returns>
176 public static implicit operator ResourceType(string resourceType)
177 {
178 return new ResourceType(resourceType);
179 }
180
181 /// <summary>
182 /// Explicitly converts a ResourceType to a string.
183 /// </summary>
184 /// <param name="resourceType">ResourceType object to convert.</param>
185 /// <returns>The resource type string.</returns>
186 /// <remarks>
187 /// Unlike <see cref="ToString" />, this conversion does not return
188 /// the common name of well-known integer resource types. Therefore,
189 /// the returned string is suitable for passing directly to Win32
190 /// resource APIs that accept resource type strings.
191 /// </remarks>
192 public static explicit operator string(ResourceType resourceType)
193 {
194 return resourceType.resourceType;
195 }
196 }
197 }
198