2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / System.ComponentModel / ComponentResourceManager.cs
blobb7291e3742c31be42b6152e960b0f2cefb51fd25
1 //
2 // System.ComponentModel.ComponentResourceManager.cs
3 //
4 // Author:
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 // Gert Driesen (drieseng@users.sourceforge.net)
7 //
8 // (C) 2003 Andreas Nahr
9 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.
32 using System;
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)
48 : base (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)
59 if (value == null)
60 throw new ArgumentNullException ("value");
61 if (objectName == null)
62 throw new ArgumentNullException ("objectName");
63 if (culture == null)
64 culture = CultureInfo.CurrentUICulture;
66 Hashtable resources = IgnoreCase ? CollectionsUtil.CreateCaseInsensitiveHashtable ()
67 : new Hashtable ();
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;
76 if (IgnoreCase)
77 bindingFlags |= BindingFlags.IgnoreCase;
79 foreach (DictionaryEntry de in resources) {
80 string resName = (string) de.Key;
81 if (!culture.CompareInfo.IsPrefix (resName, objKey, compareOptions))
82 continue;
84 string propName = resName.Substring (objKey.Length);
85 PropertyInfo pinfo = type.GetProperty (propName,
86 bindingFlags);
87 if (pinfo == null || !pinfo.CanWrite)
88 continue;
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);
103 if (rs != null) {
104 foreach (DictionaryEntry de in rs)
105 resources [(string) de.Key] = de.Value;