Update EntityFramework. Fixes #10296
[mono.git] / mcs / class / System / System.ComponentModel / ComponentResourceManager.cs
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 //
10 //
11 //
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.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Resources;
38
39 namespace System.ComponentModel
40 {
41         public class ComponentResourceManager : ResourceManager
42         {
43                 public ComponentResourceManager ()
44                 {
45                 }
46
47                 public ComponentResourceManager (Type t)
48                         : base (t)
49                 {
50                 }
51
52                 public void ApplyResources (object value, string objectName)
53                 {
54                         ApplyResources (value, objectName, (CultureInfo) null);
55                 }
56
57                 public virtual void ApplyResources (object value, string objectName, CultureInfo culture)
58                 {
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;
65
66                         Hashtable resources = IgnoreCase ? CollectionsUtil.CreateCaseInsensitiveHashtable ()
67                                 : new Hashtable ();
68                         BuildResources (culture, resources);
69
70                         string objKey = objectName + ".";
71                         CompareOptions compareOptions = IgnoreCase ?
72                                 CompareOptions.IgnoreCase : CompareOptions.None;
73                         Type type = value.GetType ();
74
75                         BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
76                         if (IgnoreCase)
77                                 bindingFlags |= BindingFlags.IgnoreCase;
78
79                         foreach (DictionaryEntry de in resources) {
80                                 string resName = (string) de.Key;
81                                 if (!culture.CompareInfo.IsPrefix (resName, objKey, compareOptions))
82                                         continue;
83
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);
92                         }
93                 }
94
95                 void BuildResources (CultureInfo culture, Hashtable resources)
96                 {
97                         // first load resources for less specific culture
98                         if (culture != culture.Parent) {
99                                 BuildResources (culture.Parent, resources);
100                         }
101
102                         ResourceSet rs = GetResourceSet (culture, true, false);
103                         if (rs != null) {
104                                 foreach (DictionaryEntry de in rs)
105                                         resources [(string) de.Key] = de.Value;
106                         }
107                 }
108         }
109 }