/// <summary>Retrieves the type name and data content of a named resource from an open resource file or stream.</summary> /// <param name="resourceName">The name of a resource.</param> /// <param name="resourceType">When this method returns, contains a string that is the type name of the retrieved type. This parameter is passed uninitialized.</param> /// <param name="resourceData">When this method returns, contains a byte array that is the binary representation of the retrieved type. This parameter is passed uninitialized.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="resourceName" /> is null.</exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="resourceName" /> does not exist.</exception> /// <exception cref="T:System.FormatException">The retrieved resource data is corrupted.</exception> /// <exception cref="T:System.InvalidOperationException">The current <see cref="T:System.Resources.ResourceReader" /> object is not initialized. The probable cause is that the <see cref="T:System.Resources.ResourceReader" /> object is closed.</exception> public void GetResourceData(string resourceName, out string resourceType, out byte[] resourceData) { if (resourceName == null) { throw new ArgumentNullException("resourceName"); } ResourceReader.ResourceEnumerator resourceEnumerator = new ResourceReader.ResourceEnumerator(this); while (resourceEnumerator.MoveNext()) { if ((string)resourceEnumerator.Key == resourceName) { this.GetResourceDataAt(resourceEnumerator.Index, out resourceType, out resourceData); return; } } throw new ArgumentException(string.Format("Specified resource not found: {0}", resourceName)); }
private object GetObject(string key, bool ignoreCase, bool isString) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (Reader == null || _resCache == null) { throw new ObjectDisposedException(null, SR.ObjectDisposed_ResourceSet); } object value = null; ResourceLocator resLocation; lock (Reader) { if (Reader == null) { throw new ObjectDisposedException(null, SR.ObjectDisposed_ResourceSet); } if (_defaultReader != null) { // Find the offset within the data section int dataPos = -1; if (_resCache.TryGetValue(key, out resLocation)) { value = resLocation.Value; dataPos = resLocation.DataPosition; } if (dataPos == -1 && value == null) { dataPos = _defaultReader.FindPosForResource(key); } if (dataPos != -1 && value == null) { Debug.Assert(dataPos >= 0, "data section offset cannot be negative!"); // Normally calling LoadString or LoadObject requires // taking a lock. Note that in this case, we took a // lock on the entire RuntimeResourceSet, which is // sufficient since we never pass this ResourceReader // to anyone else. ResourceTypeCode typeCode; if (isString) { value = _defaultReader.LoadString(dataPos); typeCode = ResourceTypeCode.String; } else { value = _defaultReader.LoadObject(dataPos, out typeCode); } resLocation = new ResourceLocator(dataPos, (ResourceLocator.CanCache(typeCode)) ? value : null); lock (_resCache) { _resCache[key] = resLocation; } } if (value != null || !ignoreCase) { return(value); // may be null } } // if (_defaultReader != null) // At this point, we either don't have our default resource reader // or we haven't found the particular resource we're looking for // and may have to search for it in a case-insensitive way. if (!_haveReadFromReader) { // If necessary, init our case insensitive hash table. if (ignoreCase && _caseInsensitiveTable == null) { _caseInsensitiveTable = new Dictionary <string, ResourceLocator>(StringComparer.OrdinalIgnoreCase); } if (_defaultReader == null) { IDictionaryEnumerator en = Reader.GetEnumerator(); while (en.MoveNext()) { DictionaryEntry entry = en.Entry; string readKey = (string)entry.Key; ResourceLocator resLoc = new ResourceLocator(-1, entry.Value); _resCache.Add(readKey, resLoc); if (ignoreCase) { _caseInsensitiveTable.Add(readKey, resLoc); } } // Only close the reader if it is NOT our default one, // since we need it around to resolve ResourceLocators. if (!ignoreCase) { Reader.Close(); } } else { Debug.Assert(ignoreCase, "This should only happen for case-insensitive lookups"); ResourceReader.ResourceEnumerator en = _defaultReader.GetEnumeratorInternal(); while (en.MoveNext()) { // Note: Always ask for the resource key before the data position. string currentKey = (string)en.Key; int dataPos = en.DataPosition; ResourceLocator resLoc = new ResourceLocator(dataPos, null); _caseInsensitiveTable.Add(currentKey, resLoc); } } _haveReadFromReader = true; } object obj = null; bool found = false; bool keyInWrongCase = false; if (_defaultReader != null) { if (_resCache.TryGetValue(key, out resLocation)) { found = true; obj = ResolveResourceLocator(resLocation, key, _resCache, keyInWrongCase); } } if (!found && ignoreCase) { if (_caseInsensitiveTable.TryGetValue(key, out resLocation)) { found = true; keyInWrongCase = true; obj = ResolveResourceLocator(resLocation, key, _resCache, keyInWrongCase); } } return(obj); } // lock(Reader) }
private object GetObject(string key, bool ignoreCase, bool isString) { if (key == null) { throw new ArgumentNullException("key"); } if ((base.Reader == null) || (this._resCache == null)) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } object obj2 = null; lock (base.Reader) { ResourceLocator locator; if (base.Reader == null) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } if (this._defaultReader != null) { int pos = -1; if (this._resCache.TryGetValue(key, out locator)) { obj2 = locator.Value; pos = locator.DataPosition; } if ((pos == -1) && (obj2 == null)) { pos = this._defaultReader.FindPosForResource(key); } if ((pos != -1) && (obj2 == null)) { ResourceTypeCode code; if (isString) { obj2 = this._defaultReader.LoadString(pos); code = ResourceTypeCode.String; } else { obj2 = this._defaultReader.LoadObject(pos, out code); } locator = new ResourceLocator(pos, ResourceLocator.CanCache(code) ? obj2 : null); lock (this._resCache) { this._resCache[key] = locator; } } if ((obj2 != null) || !ignoreCase) { return(obj2); } } if (!this._haveReadFromReader) { if (ignoreCase && (this._caseInsensitiveTable == null)) { this._caseInsensitiveTable = new Dictionary <string, ResourceLocator>(StringComparer.OrdinalIgnoreCase); } if (this._defaultReader == null) { IDictionaryEnumerator enumerator = base.Reader.GetEnumerator(); while (enumerator.MoveNext()) { DictionaryEntry entry = enumerator.Entry; string str = (string)entry.Key; ResourceLocator locator2 = new ResourceLocator(-1, entry.Value); this._resCache.Add(str, locator2); if (ignoreCase) { this._caseInsensitiveTable.Add(str, locator2); } } if (!ignoreCase) { base.Reader.Close(); } } else { ResourceReader.ResourceEnumerator enumeratorInternal = this._defaultReader.GetEnumeratorInternal(); while (enumeratorInternal.MoveNext()) { string str2 = (string)enumeratorInternal.Key; int dataPosition = enumeratorInternal.DataPosition; ResourceLocator locator3 = new ResourceLocator(dataPosition, null); this._caseInsensitiveTable.Add(str2, locator3); } } this._haveReadFromReader = true; } object obj3 = null; bool flag2 = false; bool keyInWrongCase = false; if ((this._defaultReader != null) && this._resCache.TryGetValue(key, out locator)) { flag2 = true; obj3 = this.ResolveResourceLocator(locator, key, this._resCache, keyInWrongCase); } if ((!flag2 && ignoreCase) && this._caseInsensitiveTable.TryGetValue(key, out locator)) { flag2 = true; keyInWrongCase = true; obj3 = this.ResolveResourceLocator(locator, key, this._resCache, keyInWrongCase); } return(obj3); } }
private Object GetObject(String key, bool ignoreCase, bool isString) { if (key == null) { throw new ArgumentNullException("key"); } if (Reader == null || _resCache == null) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } Contract.EndContractBlock(); Object value = null; ResourceLocator resLocation; lock (Reader) { if (Reader == null) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } if (_defaultReader != null) { BCLDebug.Log("RESMGRFILEFORMAT", "Going down fast path in RuntimeResourceSet::GetObject"); // Find the offset within the data section int dataPos = -1; if (_resCache.TryGetValue(key, out resLocation)) { value = resLocation.Value; dataPos = resLocation.DataPosition; } if (dataPos == -1 && value == null) { dataPos = _defaultReader.FindPosForResource(key); } if (dataPos != -1 && value == null) { Contract.Assert(dataPos >= 0, "data section offset cannot be negative!"); // Normally calling LoadString or LoadObject requires // taking a lock. Note that in this case, we took a // lock on the entire RuntimeResourceSet, which is // sufficient since we never pass this ResourceReader // to anyone else. ResourceTypeCode typeCode; if (isString) { value = _defaultReader.LoadString(dataPos); typeCode = ResourceTypeCode.String; } else { value = _defaultReader.LoadObject(dataPos, out typeCode); } resLocation = new ResourceLocator(dataPos, (ResourceLocator.CanCache(typeCode)) ? value : null); lock (_resCache) { _resCache[key] = resLocation; } } if (value != null || !ignoreCase) { #if LOOSELY_LINKED_RESOURCE_REFERENCE if (Assembly != null && (value is LooselyLinkedResourceReference)) { LooselyLinkedResourceReference assRef = (LooselyLinkedResourceReference)value; value = assRef.Resolve(Assembly); } #endif // LOOSELY_LINKED_RESOURCE_REFERENCE return(value); // may be null } } // if (_defaultReader != null) // At this point, we either don't have our default resource reader // or we haven't found the particular resource we're looking for // and may have to search for it in a case-insensitive way. if (!_haveReadFromReader) { // If necessary, init our case insensitive hash table. if (ignoreCase && _caseInsensitiveTable == null) { _caseInsensitiveTable = new Dictionary <String, ResourceLocator>(StringComparer.OrdinalIgnoreCase); } #if _DEBUG BCLDebug.Perf(!ignoreCase, "Using case-insensitive lookups is bad perf-wise. Consider capitalizing " + key + " correctly in your source"); #endif if (_defaultReader == null) { IDictionaryEnumerator en = Reader.GetEnumerator(); while (en.MoveNext()) { DictionaryEntry entry = en.Entry; String readKey = (String)entry.Key; ResourceLocator resLoc = new ResourceLocator(-1, entry.Value); _resCache.Add(readKey, resLoc); if (ignoreCase) { _caseInsensitiveTable.Add(readKey, resLoc); } } // Only close the reader if it is NOT our default one, // since we need it around to resolve ResourceLocators. if (!ignoreCase) { Reader.Close(); } } else { Contract.Assert(ignoreCase, "This should only happen for case-insensitive lookups"); ResourceReader.ResourceEnumerator en = _defaultReader.GetEnumeratorInternal(); while (en.MoveNext()) { // Note: Always ask for the resource key before the data position. String currentKey = (String)en.Key; int dataPos = en.DataPosition; ResourceLocator resLoc = new ResourceLocator(dataPos, null); _caseInsensitiveTable.Add(currentKey, resLoc); } } _haveReadFromReader = true; } Object obj = null; bool found = false; bool keyInWrongCase = false; if (_defaultReader != null) { if (_resCache.TryGetValue(key, out resLocation)) { found = true; obj = ResolveResourceLocator(resLocation, key, _resCache, keyInWrongCase); } } if (!found && ignoreCase) { if (_caseInsensitiveTable.TryGetValue(key, out resLocation)) { found = true; keyInWrongCase = true; obj = ResolveResourceLocator(resLocation, key, _resCache, keyInWrongCase); } } return(obj); } // lock(Reader) }
// Token: 0x06002C78 RID: 11384 RVA: 0x000A9C8C File Offset: 0x000A7E8C private object GetObject(string key, bool ignoreCase, bool isString) { if (key == null) { throw new ArgumentNullException("key"); } if (this.Reader == null || this._resCache == null) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } object obj = null; IResourceReader reader = this.Reader; object result; lock (reader) { if (this.Reader == null) { throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } ResourceLocator resourceLocator; if (this._defaultReader != null) { int num = -1; if (this._resCache.TryGetValue(key, out resourceLocator)) { obj = resourceLocator.Value; num = resourceLocator.DataPosition; } if (num == -1 && obj == null) { num = this._defaultReader.FindPosForResource(key); } if (num != -1 && obj == null) { ResourceTypeCode value; if (isString) { obj = this._defaultReader.LoadString(num); value = ResourceTypeCode.String; } else { obj = this._defaultReader.LoadObject(num, out value); } resourceLocator = new ResourceLocator(num, ResourceLocator.CanCache(value) ? obj : null); Dictionary <string, ResourceLocator> resCache = this._resCache; lock (resCache) { this._resCache[key] = resourceLocator; } } if (obj != null || !ignoreCase) { return(obj); } } if (!this._haveReadFromReader) { if (ignoreCase && this._caseInsensitiveTable == null) { this._caseInsensitiveTable = new Dictionary <string, ResourceLocator>(StringComparer.OrdinalIgnoreCase); } if (this._defaultReader == null) { IDictionaryEnumerator enumerator = this.Reader.GetEnumerator(); while (enumerator.MoveNext()) { DictionaryEntry entry = enumerator.Entry; string key2 = (string)entry.Key; ResourceLocator value2 = new ResourceLocator(-1, entry.Value); this._resCache.Add(key2, value2); if (ignoreCase) { this._caseInsensitiveTable.Add(key2, value2); } } if (!ignoreCase) { this.Reader.Close(); } } else { ResourceReader.ResourceEnumerator enumeratorInternal = this._defaultReader.GetEnumeratorInternal(); while (enumeratorInternal.MoveNext()) { string key3 = (string)enumeratorInternal.Key; int dataPosition = enumeratorInternal.DataPosition; ResourceLocator value3 = new ResourceLocator(dataPosition, null); this._caseInsensitiveTable.Add(key3, value3); } } this._haveReadFromReader = true; } object obj2 = null; bool flag3 = false; bool keyInWrongCase = false; if (this._defaultReader != null && this._resCache.TryGetValue(key, out resourceLocator)) { flag3 = true; obj2 = this.ResolveResourceLocator(resourceLocator, key, this._resCache, keyInWrongCase); } if (!flag3 && ignoreCase && this._caseInsensitiveTable.TryGetValue(key, out resourceLocator)) { keyInWrongCase = true; obj2 = this.ResolveResourceLocator(resourceLocator, key, this._resCache, keyInWrongCase); } result = obj2; } return(result); }
private object GetObject(string key, bool ignoreCase, bool isString) { if (key == null) { throw new ArgumentNullException("key"); } if (this.Reader == null || this._resCache == null) { throw new ObjectDisposedException((string)null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } object obj = (object)null; lock (this.Reader) { if (this.Reader == null) { throw new ObjectDisposedException((string)null, Environment.GetResourceString("ObjectDisposed_ResourceSet")); } ResourceLocator local_1; if (this._defaultReader != null) { int local_7 = -1; if (this._resCache.TryGetValue(key, out local_1)) { obj = local_1.Value; local_7 = local_1.DataPosition; } if (local_7 == -1 && obj == null) { local_7 = this._defaultReader.FindPosForResource(key); } if (local_7 != -1 && obj == null) { ResourceTypeCode local_8; if (isString) { obj = (object)this._defaultReader.LoadString(local_7); local_8 = ResourceTypeCode.String; } else { obj = this._defaultReader.LoadObject(local_7, out local_8); } local_1 = new ResourceLocator(local_7, ResourceLocator.CanCache(local_8) ? obj : (object)null); lock (this._resCache) this._resCache[key] = local_1; } if (obj != null || !ignoreCase) { return(obj); } } if (!this._haveReadFromReader) { if (ignoreCase && this._caseInsensitiveTable == null) { this._caseInsensitiveTable = new Dictionary <string, ResourceLocator>((IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase); } if (this._defaultReader == null) { IDictionaryEnumerator local_12 = this.Reader.GetEnumerator(); while (local_12.MoveNext()) { DictionaryEntry local_13 = local_12.Entry; string local_14 = (string)local_13.Key; ResourceLocator local_15 = new ResourceLocator(-1, local_13.Value); this._resCache.Add(local_14, local_15); if (ignoreCase) { this._caseInsensitiveTable.Add(local_14, local_15); } } if (!ignoreCase) { this.Reader.Close(); } } else { ResourceReader.ResourceEnumerator local_16 = this._defaultReader.GetEnumeratorInternal(); while (local_16.MoveNext()) { this._caseInsensitiveTable.Add((string)local_16.Key, new ResourceLocator(local_16.DataPosition, (object)null)); } } this._haveReadFromReader = true; } object local_4 = (object)null; bool local_5 = false; bool local_6 = false; if (this._defaultReader != null && this._resCache.TryGetValue(key, out local_1)) { local_5 = true; local_4 = this.ResolveResourceLocator(local_1, key, this._resCache, local_6); } if (!local_5 & ignoreCase && this._caseInsensitiveTable.TryGetValue(key, out local_1)) { bool local_6_1 = true; local_4 = this.ResolveResourceLocator(local_1, key, this._resCache, local_6_1); } return(local_4); } }