private object GetObject(string name, CultureInfo culture, bool wrapUnmanagedMemStream) { if (name == null) { throw new ArgumentNullException("name"); } if (culture == null) { culture = CultureInfo.CurrentUICulture; } if (FrameworkEventSource.IsInitialized) { FrameworkEventSource.Log.ResourceManagerLookupStarted(this.BaseNameField, this.MainAssembly, culture.Name); } ResourceFallbackManager manager = new ResourceFallbackManager(culture, this._neutralResourcesCulture, true); ResourceSet set = null; foreach (CultureInfo info in manager) { ResourceSet set2 = this.InternalGetResourceSet(info, true, true); if (set2 == null) { break; } if (set2 != set) { object obj2 = set2.GetObject(name, this._ignoreCase); if (obj2 != null) { UnmanagedMemoryStream stream = obj2 as UnmanagedMemoryStream; if ((stream != null) && wrapUnmanagedMemStream) { return(new UnmanagedMemoryStreamWrapper(stream)); } return(obj2); } set = set2; } } if (FrameworkEventSource.IsInitialized) { FrameworkEventSource.Log.ResourceManagerLookupFailed(this.BaseNameField, this.MainAssembly, culture.Name); } return(null); }
private ResourceSet InternalGetResourceSet(CultureInfo requestedCulture, bool createIfNotExists, bool tryParents, ref StackCrawlMark stackMark) { Dictionary <string, ResourceSet> localResourceSets = this._resourceSets; ResourceFallbackManager manager = new ResourceFallbackManager(requestedCulture, this._neutralResourcesCulture, tryParents); ResourceSet set = null; CultureInfo info = null; foreach (CultureInfo info2 in manager) { if (FrameworkEventSource.IsInitialized) { FrameworkEventSource.Log.ResourceManagerLookingForResourceSet(this.BaseNameField, this.MainAssembly, info2.Name); } lock (localResourceSets) { if (localResourceSets.TryGetValue(info2.Name, out set)) { if (FrameworkEventSource.IsInitialized) { FrameworkEventSource.Log.ResourceManagerFoundResourceSetInCache(this.BaseNameField, this.MainAssembly, info2.Name); } break; } } set = this.resourceGroveler.GrovelForResourceSet(info2, localResourceSets, tryParents, createIfNotExists, ref stackMark); if (set != null) { info = info2; break; } } if ((set != null) && (info != null)) { foreach (CultureInfo info3 in manager) { AddResourceSet(localResourceSets, info3.Name, ref set); if (info3 == info) { return(set); } } } return(set); }
public virtual string GetString(string name, CultureInfo culture) { if (name == null) { throw new ArgumentNullException("name"); } if (culture == null) { culture = CultureInfo.CurrentUICulture; } if (FrameworkEventSource.IsInitialized) { FrameworkEventSource.Log.ResourceManagerLookupStarted(this.BaseNameField, this.MainAssembly, culture.Name); } ResourceFallbackManager manager = new ResourceFallbackManager(culture, this._neutralResourcesCulture, true); ResourceSet set = null; foreach (CultureInfo info in manager) { ResourceSet set2 = this.InternalGetResourceSet(info, true, true); if (set2 == null) { break; } if (set2 != set) { string str = set2.GetString(name, this._ignoreCase); if (str != null) { return(str); } set = set2; } } if (FrameworkEventSource.IsInitialized) { FrameworkEventSource.Log.ResourceManagerLookupFailed(this.BaseNameField, this.MainAssembly, culture.Name); } return(null); }
// Looks up a resource value for a particular name. Looks in the // specified CultureInfo, and if not found, all parent CultureInfos. // Returns null if the resource wasn't found. // public virtual string?GetString(string name, CultureInfo?culture) { if (null == name) { throw new ArgumentNullException(nameof(name)); } #if FEATURE_APPX || ENABLE_WINRT if (_useUapResourceManagement) { // Throws WinRT hresults. Debug.Assert(_neutralResourcesCulture != null); return(GetStringFromPRI(name, culture, _neutralResourcesCulture.Name)); } #endif if (culture == null) { culture = CultureInfo.CurrentUICulture; } ResourceSet?last = GetFirstResourceSet(culture); if (last != null) { string?value = last.GetString(name, _ignoreCase); if (value != null) { return(value); } } // This is the CultureInfo hierarchy traversal code for resource // lookups, similar but necessarily orthogonal to the ResourceSet // lookup logic. ResourceFallbackManager mgr = new ResourceFallbackManager(culture, _neutralResourcesCulture, true); foreach (CultureInfo currentCultureInfo in mgr) { ResourceSet?rs = InternalGetResourceSet(currentCultureInfo, true, true); if (rs == null) { break; } if (rs != last) { string?value = rs.GetString(name, _ignoreCase); if (value != null) { // update last used ResourceSet if (_lastUsedResourceCache != null) { lock (_lastUsedResourceCache) { _lastUsedResourceCache.lastCultureName = currentCultureInfo.Name; _lastUsedResourceCache.lastResourceSet = rs; } } return(value); } last = rs; } } return(null); }
// InternalGetResourceSet is a non-threadsafe method where all the logic // for getting a resource set lives. Access to it is controlled by // threadsafe methods such as GetResourceSet, GetString, & GetObject. // This will take a minimal number of locks. protected virtual ResourceSet?InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents) { Debug.Assert(culture != null, "culture != null"); Debug.Assert(_resourceSets != null); Dictionary <string, ResourceSet> localResourceSets = _resourceSets; ResourceSet?rs = null; CultureInfo?foundCulture = null; lock (localResourceSets) { if (localResourceSets.TryGetValue(culture.Name, out rs)) { return(rs); } } ResourceFallbackManager mgr = new ResourceFallbackManager(culture, _neutralResourcesCulture, tryParents); foreach (CultureInfo currentCultureInfo in mgr) { lock (localResourceSets) { if (localResourceSets.TryGetValue(currentCultureInfo.Name, out rs)) { // we need to update the cache if we fellback if (culture != currentCultureInfo) { foundCulture = currentCultureInfo; } break; } } // InternalGetResourceSet will never be threadsafe. However, it must // be protected against reentrancy from the SAME THREAD. (ie, calling // GetSatelliteAssembly may send some window messages or trigger the // Assembly load event, which could fail then call back into the // ResourceManager). It's happened. rs = _resourceGroveler.GrovelForResourceSet(currentCultureInfo, localResourceSets, tryParents, createIfNotExists); // found a ResourceSet; we're done if (rs != null) { foundCulture = currentCultureInfo; break; } } if (rs != null && foundCulture != null) { // add entries to the cache for the cultures we have gone through // currentCultureInfo now refers to the culture that had resources. // update cultures starting from requested culture up to the culture // that had resources. foreach (CultureInfo updateCultureInfo in mgr) { AddResourceSet(localResourceSets, updateCultureInfo.Name, ref rs); // stop when we've added current or reached invariant (top of chain) if (updateCultureInfo == foundCulture) { break; } } } return(rs); }
// Looks up a resource value for a particular name. Looks in the // specified CultureInfo, and if not found, all parent CultureInfos. // Returns null if the resource wasn't found. // public virtual string GetString(string name, CultureInfo culture) { if (null == name) { throw new ArgumentNullException(nameof(name)); } #if FEATURE_APPX if (_bUsingModernResourceManagement) { // If the caller explicitly passed in a culture that was obtained by calling CultureInfo.CurrentUICulture, // null it out, so that we re-compute it. If we use modern resource lookup, we may end up getting a "better" // match, since CultureInfo objects can't represent all the different languages the AppX resource model supports. if (object.ReferenceEquals(culture, CultureInfo.CurrentUICulture)) { culture = null; } if (_PRIonAppXInitialized == false) { // Always throw if we did not fully succeed in initializing the WinRT Resource Manager. if (_PRIExceptionInfo != null && _PRIExceptionInfo.PackageSimpleName != null && _PRIExceptionInfo.ResWFile != null) { throw new MissingManifestResourceException(SR.Format(SR.MissingManifestResource_ResWFileNotLoaded, _PRIExceptionInfo.ResWFile, _PRIExceptionInfo.PackageSimpleName)); } throw new MissingManifestResourceException(SR.MissingManifestResource_NoPRIresources); } // Throws WinRT hresults. return(GetStringFromPRI(name, culture == null ? null : culture.Name, _neutralResourcesCulture.Name)); } else #endif // FEATURE_APPX { if (culture == null) { culture = CultureInfo.CurrentUICulture; } ResourceSet last = GetFirstResourceSet(culture); if (last != null) { string value = last.GetString(name, _ignoreCase); if (value != null) { return(value); } } // This is the CultureInfo hierarchy traversal code for resource // lookups, similar but necessarily orthogonal to the ResourceSet // lookup logic. ResourceFallbackManager mgr = new ResourceFallbackManager(culture, _neutralResourcesCulture, true); foreach (CultureInfo currentCultureInfo in mgr) { ResourceSet rs = InternalGetResourceSet(currentCultureInfo, true, true); if (rs == null) { break; } if (rs != last) { string value = rs.GetString(name, _ignoreCase); if (value != null) { // update last used ResourceSet if (_lastUsedResourceCache != null) { lock (_lastUsedResourceCache) { _lastUsedResourceCache.lastCultureName = currentCultureInfo.Name; _lastUsedResourceCache.lastResourceSet = rs; } } return(value); } last = rs; } } } return(null); }