The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ ComponentResourceManager

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ComponentResourceManager.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.ComponentModel
  16. {
  17.     using System;
  18.     using System.Collections;
  19.     using System.Collections.Generic;
  20.     using System.Globalization;
  21.     using System.Reflection;
  22.     using System.Resources;
  23.     using System.Security.Permissions;
  24.    
  25.     /// <devdoc>
  26.     /// The ComponentResourceManager is a resource manager object that
  27.     /// provides simple functionality for enumerating resources for
  28.     /// a component or object.
  29.     /// </devdoc>
  30.     [HostProtection(SharedState = true)]
  31.     public class ComponentResourceManager : ResourceManager
  32.     {
  33.        
  34.         private Hashtable _resourceSets;
  35.         private CultureInfo _neutralResourcesCulture;
  36.        
  37.         public ComponentResourceManager() : base()
  38.         {
  39.         }
  40.        
  41.         public ComponentResourceManager(Type t) : base(t)
  42.         {
  43.         }
  44.        
  45.        
  46.         /// <devdoc>
  47.         /// The culture of the main assembly's neutral resources. If someone is asking for this culture's resources,
  48.         /// we don't need to walk up the parent chain.
  49.         /// </devdoc>
  50.         private CultureInfo NeutralResourcesCulture {
  51.             get {
  52.                 if (_neutralResourcesCulture == null && MainAssembly != null) {
  53.                     _neutralResourcesCulture = GetNeutralResourcesLanguage(MainAssembly);
  54.                 }
  55.                
  56.                 return _neutralResourcesCulture;
  57.             }
  58.         }
  59.        
  60.         /// <devdoc>
  61.         /// This method examines all the resources for the current culture.
  62.         /// When it finds a resource with a key in the format of
  63.         /// &quot;[objectName].[property name]&quot; it will apply that resource's value
  64.         /// to the corresponding property on the object. If there is no matching
  65.         /// property the resource will be ignored.
  66.         /// </devdoc>
  67.         public void ApplyResources(object value, string objectName)
  68.         {
  69.             ApplyResources(value, objectName, null);
  70.         }
  71.        
  72.         /// <devdoc>
  73.         /// This method examines all the resources for the provided culture.
  74.         /// When it finds a resource with a key in the format of
  75.         /// &quot[objectName].[property name]&quot; it will apply that resource's value
  76.         /// to the corresponding property on the object. If there is no matching
  77.         /// property the resource will be ignored.
  78.         /// </devdoc>
  79.         public virtual void ApplyResources(object value, string objectName, CultureInfo culture)
  80.         {
  81.             if (value == null) {
  82.                 throw new ArgumentNullException("value");
  83.             }
  84.             if (objectName == null) {
  85.                 throw new ArgumentNullException("objectName");
  86.             }
  87.             if (culture == null) {
  88.                 culture = CultureInfo.CurrentUICulture;
  89.             }
  90.            
  91.             // The general case here will be to always use the same culture, so optimize for
  92.             // that. The resourceSets hashtable uses culture as a key. It's value is
  93.             // a sorted dictionary that contains ALL the culture values (so it traverses up
  94.             // the parent culture chain) for that culture. This means that if ApplyResources
  95.             // is called with different cultures there could be some redundancy in the
  96.             // table, but it allows the normal case of calling with a single culture to
  97.             // be much faster.
  98.             //
  99.            
  100.             // The reason we use a SortedDictionary here is to ensure the resources are applied
  101.             // in an order consistent with codedom deserialization.
  102.             SortedList<string, object> resources;
  103.            
  104.             if (_resourceSets == null) {
  105.                 ResourceSet dummy;
  106.                 _resourceSets = new Hashtable();
  107.                 resources = FillResources(culture, out dummy);
  108.                 _resourceSets[culture] = resources;
  109.             }
  110.             else {
  111.                 resources = (SortedList<string, object>)_resourceSets[culture];
  112.                 if (resources == null || (resources.Comparer.Equals(StringComparer.OrdinalIgnoreCase) != IgnoreCase)) {
  113.                     ResourceSet dummy;
  114.                     resources = FillResources(culture, out dummy);
  115.                     _resourceSets[culture] = resources;
  116.                 }
  117.             }
  118.            
  119.             BindingFlags flags = BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance;
  120.            
  121.             if (IgnoreCase) {
  122.                 flags |= BindingFlags.IgnoreCase;
  123.             }
  124.            
  125.             bool componentReflect = false;
  126.             if (value is IComponent) {
  127.                 ISite site = ((IComponent)value).Site;
  128.                 if (site != null && site.DesignMode) {
  129.                     componentReflect = true;
  130.                 }
  131.             }
  132.            
  133.             foreach (KeyValuePair<string, object> kvp in resources) {
  134.                
  135.                 // See if this key matches our object.
  136.                 //
  137.                 string key = kvp.Key;
  138.                 if (key == null) {
  139.                     continue;
  140.                 }
  141.                
  142.                 if (IgnoreCase) {
  143.                     if (string.Compare(key, 0, objectName, 0, objectName.Length, StringComparison.OrdinalIgnoreCase) != 0) {
  144.                         continue;
  145.                     }
  146.                 }
  147.                 else {
  148.                     if (string.CompareOrdinal(key, 0, objectName, 0, objectName.Length) != 0) {
  149.                         continue;
  150.                     }
  151.                 }
  152.                
  153.                 // Character after objectName.Length should be a ".", or else we should continue.
  154.                 //
  155.                 int idx = objectName.Length;
  156.                 if (key.Length <= idx || key[idx] != '.') {
  157.                     continue;
  158.                 }
  159.                
  160.                 // Bypass type descriptor if we are not in design mode. TypeDescriptor does an attribute
  161.                 // scan which is quite expensive.
  162.                 //
  163.                 string propName = key.Substring(idx + 1);
  164.                
  165.                 if (componentReflect) {
  166.                     PropertyDescriptor prop = TypeDescriptor.GetProperties(value).Find(propName, IgnoreCase);
  167.                    
  168.                     if (prop != null && !prop.IsReadOnly && (kvp.Value == null || prop.PropertyType.IsInstanceOfType(kvp.Value))) {
  169.                         prop.SetValue(value, kvp.Value);
  170.                     }
  171.                 }
  172.                 else {
  173.                     PropertyInfo prop = null;
  174.                    
  175.                     try {
  176.                         prop = value.GetType().GetProperty(propName, flags);
  177.                     }
  178.                     catch (AmbiguousMatchException) {
  179.                         // Looks like we ran into a conflict between a declared property and an inherited one.
  180.                         // In such cases, we choose the most declared one.
  181.                         Type t = value.GetType();
  182.                         do {
  183.                             prop = t.GetProperty(propName, flags | BindingFlags.DeclaredOnly);
  184.                             t = t.BaseType;
  185.                         }
  186.                         while (prop == null && t != null && t != typeof(object));
  187.                     }
  188.                    
  189.                     if (prop != null && prop.CanWrite && (kvp.Value == null || prop.PropertyType.IsInstanceOfType(kvp.Value))) {
  190.                         prop.SetValue(value, kvp.Value, null);
  191.                     }
  192.                 }
  193.             }
  194.         }
  195.        
  196.         /// <devdoc>
  197.         /// Recursive routine that creates a resource hashtable
  198.         /// populated with resources for culture and all parent
  199.         /// cultures.
  200.         /// </devdoc>
  201.         private SortedList<string, object> FillResources(CultureInfo culture, out ResourceSet resourceSet)
  202.         {
  203.            
  204.             SortedList<string, object> sd;
  205.             ResourceSet parentResourceSet = null;
  206.            
  207.             // Traverse parents first, so we always replace more
  208.             // specific culture values with less specific.
  209.             //
  210.             if (!culture.Equals(CultureInfo.InvariantCulture) && !culture.Equals(NeutralResourcesCulture)) {
  211.                 sd = FillResources(culture.Parent, out parentResourceSet);
  212.             }
  213.             else {
  214.                
  215.                 // We're at the bottom, so create the sorted dictionary
  216.                 //
  217.                 if (IgnoreCase) {
  218.                     sd = new SortedList<string, object>(StringComparer.OrdinalIgnoreCase);
  219.                 }
  220.                 else {
  221.                     sd = new SortedList<string, object>(StringComparer.Ordinal);
  222.                 }
  223.             }
  224.            
  225.             // Now walk culture's resource set. Another thing we
  226.             // do here is ask ResourceManager to traverse up the
  227.             // parent chain. We do NOT want to do this because
  228.             // we are trawling up the parent chain ourselves, but by
  229.             // passing in true for the second parameter the resource
  230.             // manager will cache the culture it did find, so when we
  231.             // do recurse all missing resources will be filled in
  232.             // so we are very fast. That's why we remember what our
  233.             // parent resource set's instance was -- if they are the
  234.             // same, we're looking at a cache we've already applied.
  235.             //
  236.             resourceSet = GetResourceSet(culture, true, true);
  237.             if (resourceSet != null && !object.ReferenceEquals(resourceSet, parentResourceSet)) {
  238.                 foreach (DictionaryEntry de in resourceSet) {
  239.                     sd[(string)de.Key] = de.Value;
  240.                 }
  241.             }
  242.            
  243.             return sd;
  244.         }
  245.        
  246.        
  247.     }
  248. }

Developer Fusion