2 // System.ComponentModel.ComponentResourceManager.cs
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 // Gert Driesen (drieseng@users.sourceforge.net)
8 // (C) 2003 Andreas Nahr
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Resources;
39 namespace System.ComponentModel
41 public class ComponentResourceManager : ResourceManager
43 public ComponentResourceManager ()
47 public ComponentResourceManager (Type t)
52 public void ApplyResources (object value, string objectName)
54 ApplyResources (value, objectName, (CultureInfo) null);
57 public virtual void ApplyResources (object value, string objectName, CultureInfo culture)
60 throw new ArgumentNullException ("value");
61 if (objectName == null)
62 throw new ArgumentNullException ("objectName");
64 culture = CultureInfo.CurrentUICulture;
66 Hashtable resources = IgnoreCase ? CollectionsUtil.CreateCaseInsensitiveHashtable ()
68 BuildResources (culture, resources);
70 string objKey = objectName + ".";
71 CompareOptions compareOptions = IgnoreCase ?
72 CompareOptions.IgnoreCase : CompareOptions.None;
73 Type type = value.GetType ();
75 BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
77 bindingFlags |= BindingFlags.IgnoreCase;
79 foreach (DictionaryEntry de in resources) {
80 string resName = (string) de.Key;
81 if (!culture.CompareInfo.IsPrefix (resName, objKey, compareOptions))
84 string propName = resName.Substring (objKey.Length);
85 PropertyInfo pinfo = type.GetProperty (propName,
87 if (pinfo == null || !pinfo.CanWrite)
89 object resValue = de.Value;
90 if (resValue == null || pinfo.PropertyType.IsInstanceOfType (resValue))
91 pinfo.SetValue (value, resValue, null);
95 void BuildResources (CultureInfo culture, Hashtable resources)
97 // first load resources for less specific culture
98 if (culture != culture.Parent) {
99 BuildResources (culture.Parent, resources);
102 ResourceSet rs = GetResourceSet (culture, true, false);
104 foreach (DictionaryEntry de in rs)
105 resources [(string) de.Key] = de.Value;