2 // System.Resources.ResourceManager.cs
5 // Duncan Mak (duncan@ximian.com)
7 // 2001 (C) Ximian, Inc. http://www.ximian.com
10 using System.Collections;
11 using System.Reflection;
12 using System.Globalization;
14 namespace System.Resources
17 public class ResourceManager
19 public static readonly int HeaderVersionNumber;
20 // public static readonly int MagicNumber = 0xBEEFCACE;
22 protected string BaseNameField;
23 protected Assembly MainAssembly;
24 protected Hashtable ResourceSets;
26 private bool ignoreCase;
27 private Type resourceSetType;
30 public ResourceManager () {}
32 public ResourceManager (Type resourceSource)
34 if (resourceSource == null)
35 throw new ArgumentNullException ("resourceSource is null.");
37 BaseNameField = resourceSource.FullName;
38 MainAssembly = resourceSource.Assembly;
41 resourceSetType = resourceSource;
44 public ResourceManager (string baseName, Assembly assembly)
46 if (baseName == null || assembly == null)
47 throw new ArgumentNullException ("The arguments are null.");
49 BaseNameField = baseName;
50 MainAssembly = assembly;
52 resourceSetType = typeof (ResourceSet);
55 public ResourceManager (string baseName, Assembly assembly, Type usingResourceSet)
57 if (baseName == null || assembly == null)
58 throw new ArgumentNullException ("The arguments are null.");
60 BaseNameField = baseName;
61 MainAssembly = assembly;
63 if (usingResourceSet == null) // defaults resourceSet type.
64 resourceSetType = typeof (ResourceSet);
66 if (!usingResourceSet.IsSubclassOf (typeof (ResourceSet)))
67 throw new ArgumentException ("Type must be from ResourceSet.");
73 public static ResourceManager CreateFileBasedResourceManager (string baseName,
74 string resourceDir, Type usingResourceSet)
79 public virtual string BaseName
81 get { return BaseNameField; }
84 public virtual bool IgnoreCase
86 get { return ignoreCase; }
87 set { ignoreCase = value; }
90 public virtual Type ResourceSetType
92 get { return resourceSetType; }
96 public virtual ResourceSet GetResourceSet (CultureInfo culture,
97 bool createIfNotExists, bool tryParents)
101 throw new ArgumentNullException ("CultureInfo is a null reference.");
106 public virtual string GetString (string name)
109 throw new ArgumentNullException ("Name is null.");
110 if (ResourceSets.Contains (name)) {
111 if (!(ResourceSets[name] is string))
112 throw new InvalidOperationException ("The resource is " +
114 return ResourceSets[name].ToString();
120 public virtual string GetString (string name, CultureInfo culture)
123 throw new ArgumentNullException ("Name is null.");
127 protected virtual string GetResourceFileName (CultureInfo culture)
129 return culture.Name + ".resources";
133 protected virtual ResourceSet InternalGetResourceSet (CultureInfo culture,
134 bool Createifnotexists, bool tryParents)
139 public virtual void ReleaseAllResources ()
141 foreach (ResourceSet r in ResourceSets)
145 protected static CultureInfo GetNeutralResourcesLanguage (Assembly a)
147 foreach (Attribute attribute in a.GetCustomAttributes (false)) {
148 if (attribute is NeutralResourcesLanguageAttribute)
149 return new CultureInfo ((attribute as NeutralResourcesLanguageAttribute).CultureName);
154 protected static Version GetSatelliteContractVersion (Assembly a)
156 foreach (Attribute attribute in a.GetCustomAttributes (false)) {
157 if (attribute is SatelliteContractVersionAttribute)
158 return new Version ((attribute as SatelliteContractVersionAttribute).Version);
160 return null; // return null if no version was found.