mscorlib(4.0.0.0) API with additions
ResourceSet.cs
1 using System.Collections;
2 using System.IO;
4 using System.Security;
5 
6 namespace System.Resources
7 {
10  [Serializable]
11  [ComVisible(true)]
13  {
15  [NonSerialized]
17 
19  protected Hashtable Table;
20 
21  private Hashtable _caseInsensitiveTable;
22 
24  protected ResourceSet()
25  {
26  CommonInit();
27  }
28 
29  internal ResourceSet(bool junk)
30  {
31  }
32 
36  public ResourceSet(string fileName)
37  {
38  Reader = new ResourceReader(fileName);
39  CommonInit();
40  ReadResources();
41  }
42 
47  [SecurityCritical]
48  public ResourceSet(Stream stream)
49  {
50  Reader = new ResourceReader(stream);
51  CommonInit();
52  ReadResources();
53  }
54 
59  {
60  if (reader == null)
61  {
62  throw new ArgumentNullException("reader");
63  }
64  Reader = reader;
65  CommonInit();
66  ReadResources();
67  }
68 
69  private void CommonInit()
70  {
71  Table = new Hashtable();
72  }
73 
75  public virtual void Close()
76  {
77  Dispose(disposing: true);
78  }
79 
82  protected virtual void Dispose(bool disposing)
83  {
84  if (disposing)
85  {
86  IResourceReader reader = Reader;
87  Reader = null;
88  reader?.Close();
89  }
90  Reader = null;
91  _caseInsensitiveTable = null;
92  Table = null;
93  }
94 
96  public void Dispose()
97  {
98  Dispose(disposing: true);
99  }
100 
103  public virtual Type GetDefaultReader()
104  {
105  return typeof(ResourceReader);
106  }
107 
110  public virtual Type GetDefaultWriter()
111  {
112  return typeof(ResourceWriter);
113  }
114 
118  [ComVisible(false)]
120  {
121  return GetEnumeratorHelper();
122  }
123 
127  {
128  return GetEnumeratorHelper();
129  }
130 
131  private IDictionaryEnumerator GetEnumeratorHelper()
132  {
133  Hashtable table = Table;
134  if (table == null)
135  {
136  throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet"));
137  }
138  return table.GetEnumerator();
139  }
140 
147  public virtual string GetString(string name)
148  {
149  object objectInternal = GetObjectInternal(name);
150  try
151  {
152  return (string)objectInternal;
153  }
154  catch (InvalidCastException)
155  {
156  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ResourceNotString_Name", name));
157  }
158  }
159 
167  public virtual string GetString(string name, bool ignoreCase)
168  {
169  object objectInternal = GetObjectInternal(name);
170  string text;
171  try
172  {
173  text = (string)objectInternal;
174  }
175  catch (InvalidCastException)
176  {
177  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ResourceNotString_Name", name));
178  }
179  if (text != null || !ignoreCase)
180  {
181  return text;
182  }
183  objectInternal = GetCaseInsensitiveObjectInternal(name);
184  try
185  {
186  return (string)objectInternal;
187  }
188  catch (InvalidCastException)
189  {
190  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ResourceNotString_Name", name));
191  }
192  }
193 
199  public virtual object GetObject(string name)
200  {
201  return GetObjectInternal(name);
202  }
203 
210  public virtual object GetObject(string name, bool ignoreCase)
211  {
212  object objectInternal = GetObjectInternal(name);
213  if (objectInternal != null || !ignoreCase)
214  {
215  return objectInternal;
216  }
217  return GetCaseInsensitiveObjectInternal(name);
218  }
219 
221  protected virtual void ReadResources()
222  {
224  while (enumerator.MoveNext())
225  {
226  object value = enumerator.Value;
227  Table.Add(enumerator.Key, value);
228  }
229  }
230 
231  private object GetObjectInternal(string name)
232  {
233  if (name == null)
234  {
235  throw new ArgumentNullException("name");
236  }
237  Hashtable table = Table;
238  if (table == null)
239  {
240  throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet"));
241  }
242  return table[name];
243  }
244 
245  private object GetCaseInsensitiveObjectInternal(string name)
246  {
247  Hashtable table = Table;
248  if (table == null)
249  {
250  throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet"));
251  }
252  Hashtable hashtable = _caseInsensitiveTable;
253  if (hashtable == null)
254  {
255  hashtable = new Hashtable(StringComparer.OrdinalIgnoreCase);
256  IDictionaryEnumerator enumerator = table.GetEnumerator();
257  while (enumerator.MoveNext())
258  {
259  hashtable.Add(enumerator.Key, enumerator.Value);
260  }
261  _caseInsensitiveTable = hashtable;
262  }
263  return hashtable[name];
264  }
265  }
266 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
virtual string GetString(string name, bool ignoreCase)
Searches for a T:System.String resource with the specified name in a case-insensitive manner,...
Definition: ResourceSet.cs:167
bool MoveNext()
Advances the enumerator to the next element of the collection.
object Key
Gets the key of the current dictionary entry.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Definition: __Canon.cs:3
The exception that is thrown for invalid casting or explicit conversion.
IResourceReader Reader
Indicates the T:System.Resources.IResourceReader used to read the resources.
Definition: ResourceSet.cs:16
new IDictionaryEnumerator GetEnumerator()
Returns a dictionary enumerator of the resources for this reader.
void Dispose()
Disposes of the resources (other than memory) used by the current instance of T:System....
Definition: ResourceSet.cs:96
Provides the base functionality for reading data from resource files.
ResourceSet(string fileName)
Creates a new instance of the T:System.Resources.ResourceSet class using the system default T:System....
Definition: ResourceSet.cs:36
The exception that is thrown when an operation is performed on a disposed object.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual Type GetDefaultReader()
Returns the preferred resource reader class for this kind of T:System.Resources.ResourceSet.
Definition: ResourceSet.cs:103
virtual IDictionaryEnumerator GetEnumerator()
Returns an T:System.Collections.IDictionaryEnumerator that can iterate through the T:System....
Definition: ResourceSet.cs:119
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
void Close()
Closes the resource reader after releasing any resources associated with it.
virtual object GetObject(string name, bool ignoreCase)
Searches for a resource object with the specified name in a case-insensitive manner,...
Definition: ResourceSet.cs:210
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
ResourceSet()
Initializes a new instance of the T:System.Resources.ResourceSet class with default properties.
Definition: ResourceSet.cs:24
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
Enumerates the resources in a binary resources (.resources) file by reading sequential resource name/...
Hashtable Table
The T:System.Collections.Hashtable in which the resources are stored.
Definition: ResourceSet.cs:19
Writes resources in the system-default format to an output file or an output stream....
virtual string GetString(string name)
Searches for a T:System.String resource with the specified name.
Definition: ResourceSet.cs:147
ResourceSet(IResourceReader reader)
Creates a new instance of the T:System.Resources.ResourceSet class using the specified resource reade...
Definition: ResourceSet.cs:58
virtual void ReadResources()
Reads all the resources and stores them in a T:System.Collections.Hashtable indicated in the F:System...
Definition: ResourceSet.cs:221
object Value
Gets the value of the current dictionary entry.
Specifies that the class can be serialized.
Stores all the resources localized for one particular culture, ignoring all other cultures,...
Definition: ResourceSet.cs:12
Enumerates the elements of a nongeneric dictionary.
The exception that is thrown when a method call is invalid for the object's current state.
virtual void Dispose(bool disposing)
Releases resources (other than memory) associated with the current instance, closing internal managed...
Definition: ResourceSet.cs:82
ResourceSet(Stream stream)
Creates a new instance of the T:System.Resources.ResourceSet class using the system default T:System....
Definition: ResourceSet.cs:48
virtual object GetObject(string name)
Searches for a resource object with the specified name.
Definition: ResourceSet.cs:199
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
virtual void Close()
Closes and releases any resources used by this T:System.Resources.ResourceSet.
Definition: ResourceSet.cs:75
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16
virtual Type GetDefaultWriter()
Returns the preferred resource writer class for this kind of T:System.Resources.ResourceSet.
Definition: ResourceSet.cs:110