Ejemplo n.º 1
0
        private ResourceSet InternalGetResourceSet(CultureInfo requestedCulture, bool createIfNotExists, bool tryParents, ref StackCrawlMark stackMark)
        {
            Dictionary<String, ResourceSet> localResourceSets = _resourceSets;
            ResourceSet rs = null;
            CultureInfo foundCulture = null;
            lock (localResourceSets) {
                if (localResourceSets.TryGetValue(requestedCulture.Name, out rs)) {
#if !FEATURE_CORECLR
                    if (FrameworkEventSource.IsInitialized) {
                        FrameworkEventSource.Log.ResourceManagerFoundResourceSetInCache(BaseNameField, MainAssembly, requestedCulture.Name);
                    }
#endif
                    return rs;
                }
            }

            ResourceFallbackManager mgr = new ResourceFallbackManager(requestedCulture, _neutralResourcesCulture, tryParents);

            foreach (CultureInfo currentCultureInfo in mgr)
            {
#if !FEATURE_CORECLR
                if (FrameworkEventSource.IsInitialized)
                {
                    FrameworkEventSource.Log.ResourceManagerLookingForResourceSet(BaseNameField, MainAssembly, currentCultureInfo.Name);
                }
#endif
                lock(localResourceSets) {
                    if (localResourceSets.TryGetValue(currentCultureInfo.Name, out rs)) {
#if !FEATURE_CORECLR
                        if (FrameworkEventSource.IsInitialized)
                        {
                            FrameworkEventSource.Log.ResourceManagerFoundResourceSetInCache(BaseNameField, MainAssembly, currentCultureInfo.Name);
                        }
#endif
                        // we need to update the cache if we fellback
                        if(requestedCulture != 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, ref stackMark);

                // 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;
        }
Ejemplo n.º 2
0
        // 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("name");
            Contract.EndContractBlock();

#if FEATURE_APPX
            if(s_IsAppXModel)
            {
                 // If the caller explictily 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.
                 // For classic resources, this causes us to ignore the languages list and instead use the older Win32 behavior,
                 // which is the design choice we've made. (See the call a little later to GetCurrentUICultureNoAppX()).
                 if(Object.ReferenceEquals(culture, CultureInfo.CurrentUICulture))
                 {
                     culture = null;
                 }              
            }

            if (_bUsingModernResourceManagement)
            {
                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(Environment.GetResourceString("MissingManifestResource_ResWFileNotLoaded", _PRIExceptionInfo._ResWFile, _PRIExceptionInfo._PackageSimpleName));

                    throw new MissingManifestResourceException(Environment.GetResourceString("MissingManifestResource_NoPRIresources"));
                }
            
                // Throws WinRT hresults.
                return GetStringFromPRI(name,
                                        culture == null ? null : culture.Name,
                                        _neutralResourcesCulture.Name);
            }
            else
#endif // FEATURE_APPX
            {
                if (null==culture) {
                    // When running inside AppX we want to ignore the languages list when trying to come up with our CurrentUICulture.
                    // This line behaves the same way as CultureInfo.CurrentUICulture would have in .NET 4
                    culture = Thread.CurrentThread.GetCurrentUICultureNoAppX();
                }
    
#if !FEATURE_CORECLR
                if (FrameworkEventSource.IsInitialized)
                {
                    FrameworkEventSource.Log.ResourceManagerLookupStarted(BaseNameField, MainAssembly, culture.Name);
                }
#endif
                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;
                    }
                }

#if !FEATURE_CORECLR
                if (FrameworkEventSource.IsInitialized)
                {
                    FrameworkEventSource.Log.ResourceManagerLookupFailed(BaseNameField, MainAssembly, culture.Name);
                }
#endif
            }

            return null;
        }
Ejemplo n.º 3
0
        private Object GetObject(String name, CultureInfo culture, bool wrapUnmanagedMemStream)
        {
            if (null==name)
                throw new ArgumentNullException("name");
            Contract.EndContractBlock();

#if FEATURE_APPX
            if(s_IsAppXModel)
            {
                 // If the caller explictily passed in a culture that was obtained by calling CultureInfo.CurrentUICulture,
                 // null it out, so that we re-compute it based on the Win32 value and not the AppX language list value.
                 // (See the call a little later to GetCurrentUICultureNoAppX()).
                 if(Object.ReferenceEquals(culture, CultureInfo.CurrentUICulture))
                 {
                     culture = null;
                 }              
            }
#endif

            if (null==culture) {
                // When running inside AppX we want to ignore the languages list when trying to come up with our CurrentUICulture.
                // This line behaves the same way as CultureInfo.CurrentUICulture would have in .NET 4
                culture = Thread.CurrentThread.GetCurrentUICultureNoAppX();
            }

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized)
            {
                FrameworkEventSource.Log.ResourceManagerLookupStarted(BaseNameField, MainAssembly, culture.Name);
            }
#endif
            ResourceSet last = GetFirstResourceSet(culture);
            if (last != null)
            {
                Object value = last.GetObject(name, _ignoreCase);

                if (value != null) 
                {
                    UnmanagedMemoryStream stream = value as UnmanagedMemoryStream;
                    if (stream != null && wrapUnmanagedMemStream)
                        return new UnmanagedMemoryStreamWrapper(stream);
                    else
                        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) {
                // Note: Technically this method should be passed in a stack crawl mark that we then pass
                // to InternalGetResourceSet for ensuring we demand permissions to read your private resources
                // if you're reading resources from an assembly other than yourself.  But, we must call our
                // three argument overload (without the stack crawl mark) for compatibility.  After 
                // consideration, we aren't worried about the security impact.
                ResourceSet rs = InternalGetResourceSet(currentCultureInfo, true, true);
                if (rs == null)
                    break;

                if (rs != last) {
                    Object value = rs.GetObject(name, _ignoreCase);
                    if (value != null) {
                        // update the last used ResourceSet
                        if (_lastUsedResourceCache != null) {
                            lock (_lastUsedResourceCache) {
                                _lastUsedResourceCache.lastCultureName = currentCultureInfo.Name;
                                _lastUsedResourceCache.lastResourceSet = rs;
                            }
                        }

                        UnmanagedMemoryStream stream = value as UnmanagedMemoryStream;
                        if (stream != null && wrapUnmanagedMemStream)
                            return new UnmanagedMemoryStreamWrapper(stream);
                        else
                            return value;
                    }

                    last = rs;
                }
            }

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized)
            {
                FrameworkEventSource.Log.ResourceManagerLookupFailed(BaseNameField, MainAssembly, culture.Name);
            }
#endif
            return null;
        }
Ejemplo n.º 4
0
        [System.Security.SecuritySafeCritical]  // auto-generated 
        private Object GetObject(String name, CultureInfo culture, bool wrapUnmanagedMemStream) 
        {
            if (null==name) 
                throw new ArgumentNullException("name");
            if (null==culture) {
                culture = CultureInfo.CurrentUICulture;
            } 

#if !FEATURE_CORECLR 
            if (FrameworkEventSource.IsInitialized) 
            {
                FrameworkEventSource.Log.ResourceManagerLookupStarted(BaseNameField, MainAssembly, culture.Name); 
            }
#endif

            // 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); 
            ResourceSet last = null;
            foreach (CultureInfo currentCultureInfo in mgr) { 
                // Note: Technically this method should be passed in a stack crawl mark that we then pass
                // to InternalGetResourceSet for ensuring we demand permissions to read your private resources
                // if you're reading resources from an assembly other than yourself.  But, we must call our
                // three argument overload (without the stack crawl mark) for compatibility.  After 
                // consideration, we aren't worried about the security impact.
                ResourceSet rs = InternalGetResourceSet(currentCultureInfo, true, true); 
                if (rs == null) 
                    break;
 
                if (rs != last) {
                    Object value = rs.GetObject(name, _ignoreCase);
                    if (value != null) {
                        UnmanagedMemoryStream stream = value as UnmanagedMemoryStream; 
                        if (stream != null && wrapUnmanagedMemStream)
                            return new UnmanagedMemoryStreamWrapper(stream); 
                        else 
                            return value;
                    } 

                    last = rs;
                }
            } 

#if !FEATURE_CORECLR 
            if (FrameworkEventSource.IsInitialized) 
            {
                FrameworkEventSource.Log.ResourceManagerLookupFailed(BaseNameField, MainAssembly, culture.Name); 
            }
#endif
            return null;
        } 
Ejemplo n.º 5
0
        [System.Security.SecuritySafeCritical]  // auto-generated 
        public virtual String GetString(String name, CultureInfo culture) {
            if (null==name)
                throw new ArgumentNullException("name");
            if (null==culture) { 
                culture = CultureInfo.CurrentUICulture;
            } 
 
#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized) 
            {
                FrameworkEventSource.Log.ResourceManagerLookupStarted(BaseNameField, MainAssembly, culture.Name);
            }
#endif 

            // 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); 
            ResourceSet last = null;
            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) 
                        return value;

                    last = rs;
                } 
            }
 
#if !FEATURE_CORECLR 
            if (FrameworkEventSource.IsInitialized)
            { 
                FrameworkEventSource.Log.ResourceManagerLookupFailed(BaseNameField, MainAssembly, culture.Name);
            }
#endif
 
            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;
 }
 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;
 }