mscorlib(4.0.0.0) API with additions
ResourceManager.cs
1 using System.Collections;
5 using System.IO;
6 using System.Reflection;
10 using System.Security;
11 using System.Text;
12 using System.Threading;
13 
14 namespace System.Resources
15 {
18  [Serializable]
19  [ComVisible(true)]
20  [__DynamicallyInvokable]
21  public class ResourceManager
22  {
23  internal class CultureNameResourceSetPair
24  {
25  public string lastCultureName;
26 
27  public ResourceSet lastResourceSet;
28  }
29 
30  internal class ResourceManagerMediator
31  {
32  private ResourceManager _rm;
33 
34  internal string ModuleDir => _rm.moduleDir;
35 
36  internal Type LocationInfo => _rm._locationInfo;
37 
38  internal Type UserResourceSet => _rm._userResourceSet;
39 
40  internal string BaseNameField => _rm.BaseNameField;
41 
42  internal CultureInfo NeutralResourcesCulture
43  {
44  get
45  {
46  return _rm._neutralResourcesCulture;
47  }
48  set
49  {
50  _rm._neutralResourcesCulture = value;
51  }
52  }
53 
54  internal bool LookedForSatelliteContractVersion
55  {
56  get
57  {
58  return _rm._lookedForSatelliteContractVersion;
59  }
60  set
61  {
62  _rm._lookedForSatelliteContractVersion = value;
63  }
64  }
65 
66  internal Version SatelliteContractVersion
67  {
68  get
69  {
70  return _rm._satelliteContractVersion;
71  }
72  set
73  {
74  _rm._satelliteContractVersion = value;
75  }
76  }
77 
78  internal UltimateResourceFallbackLocation FallbackLoc
79  {
80  get
81  {
82  return _rm.FallbackLocation;
83  }
84  set
85  {
86  _rm._fallbackLoc = value;
87  }
88  }
89 
90  internal RuntimeAssembly CallingAssembly => _rm.m_callingAssembly;
91 
92  internal RuntimeAssembly MainAssembly => (RuntimeAssembly)_rm.MainAssembly;
93 
94  internal string BaseName => _rm.BaseName;
95 
96  internal ResourceManagerMediator(ResourceManager rm)
97  {
98  if (rm == null)
99  {
100  throw new ArgumentNullException("rm");
101  }
102  _rm = rm;
103  }
104 
105  internal string GetResourceFileName(CultureInfo culture)
106  {
107  return _rm.GetResourceFileName(culture);
108  }
109 
110  internal Version ObtainSatelliteContractVersion(Assembly a)
111  {
112  return GetSatelliteContractVersion(a);
113  }
114 
115  [SecurityCritical]
116  internal bool TryLookingForSatellite(CultureInfo lookForCulture)
117  {
118  return _rm.TryLookingForSatellite(lookForCulture);
119  }
120  }
121 
123  protected string BaseNameField;
124 
126  [Obsolete("call InternalGetResourceSet instead")]
128 
129  [NonSerialized]
130  private Dictionary<string, ResourceSet> _resourceSets;
131 
132  private string moduleDir;
133 
136 
137  private Type _locationInfo;
138 
139  private Type _userResourceSet;
140 
141  private CultureInfo _neutralResourcesCulture;
142 
143  [NonSerialized]
144  private CultureNameResourceSetPair _lastUsedResourceCache;
145 
146  private bool _ignoreCase;
147 
148  private bool UseManifest;
149 
150  [OptionalField(VersionAdded = 1)]
151  private bool UseSatelliteAssem;
152 
153  private static volatile Hashtable _installedSatelliteInfo;
154 
155  private static volatile bool _checkedConfigFile;
156 
157  [OptionalField]
158  private UltimateResourceFallbackLocation _fallbackLoc;
159 
160  [OptionalField]
161  private Version _satelliteContractVersion;
162 
163  [OptionalField]
164  private bool _lookedForSatelliteContractVersion;
165 
166  [OptionalField(VersionAdded = 1)]
167  private Assembly _callingAssembly;
168 
169  [OptionalField(VersionAdded = 4)]
170  private RuntimeAssembly m_callingAssembly;
171 
172  [NonSerialized]
173  private IResourceGroveler resourceGroveler;
174 
176  public static readonly int MagicNumber = -1091581234;
177 
179  public static readonly int HeaderVersionNumber = 1;
180 
181  private static readonly Type _minResourceSet = typeof(ResourceSet);
182 
183  internal static readonly string ResReaderTypeName = typeof(ResourceReader).FullName;
184 
185  internal static readonly string ResSetTypeName = typeof(RuntimeResourceSet).FullName;
186 
187  internal static readonly string MscorlibName = typeof(ResourceReader).Assembly.FullName;
188 
189  internal const string ResFileExtension = ".resources";
190 
191  internal const int ResFileExtensionLength = 10;
192 
193  internal static readonly int DEBUG = 0;
194 
195  private static volatile bool s_IsAppXModel;
196 
197  [NonSerialized]
198  private bool _bUsingModernResourceManagement;
199 
200  [NonSerialized]
201  [SecurityCritical]
202  private WindowsRuntimeResourceManagerBase _WinRTResourceManager;
203 
204  [NonSerialized]
205  private bool _PRIonAppXInitialized;
206 
207  [NonSerialized]
208  private PRIExceptionInfo _PRIExceptionInfo;
209 
212  public virtual string BaseName => BaseNameField;
213 
217  public virtual bool IgnoreCase
218  {
219  get
220  {
221  return _ignoreCase;
222  }
223  set
224  {
225  _ignoreCase = value;
226  }
227  }
228 
231  public virtual Type ResourceSetType
232  {
233  get
234  {
235  if (!(_userResourceSet == null))
236  {
237  return _userResourceSet;
238  }
239  return typeof(RuntimeResourceSet);
240  }
241  }
242 
246  {
247  get
248  {
249  return _fallbackLoc;
250  }
251  set
252  {
253  _fallbackLoc = value;
254  }
255  }
256 
257  [MethodImpl(MethodImplOptions.NoInlining)]
258  private void Init()
259  {
260  m_callingAssembly = (RuntimeAssembly)Assembly.GetCallingAssembly();
261  }
262 
264  protected ResourceManager()
265  {
266  Init();
267  _lastUsedResourceCache = new CultureNameResourceSetPair();
268  ResourceManagerMediator mediator = new ResourceManagerMediator(this);
269  resourceGroveler = new ManifestBasedResourceGroveler(mediator);
270  }
271 
272  private ResourceManager(string baseName, string resourceDir, Type usingResourceSet)
273  {
274  if (baseName == null)
275  {
276  throw new ArgumentNullException("baseName");
277  }
278  if (resourceDir == null)
279  {
280  throw new ArgumentNullException("resourceDir");
281  }
282  BaseNameField = baseName;
283  moduleDir = resourceDir;
284  _userResourceSet = usingResourceSet;
285  ResourceSets = new Hashtable();
286  _resourceSets = new Dictionary<string, ResourceSet>();
287  _lastUsedResourceCache = new CultureNameResourceSetPair();
288  UseManifest = false;
289  ResourceManagerMediator mediator = new ResourceManagerMediator(this);
290  resourceGroveler = new FileBasedResourceGroveler(mediator);
291  if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled())
292  {
293  CultureInfo invariantCulture = CultureInfo.InvariantCulture;
294  string resourceFileName = GetResourceFileName(invariantCulture);
295  if (resourceGroveler.HasNeutralResources(invariantCulture, resourceFileName))
296  {
297  FrameworkEventSource.Log.ResourceManagerNeutralResourcesFound(BaseNameField, MainAssembly, resourceFileName);
298  }
299  else
300  {
301  FrameworkEventSource.Log.ResourceManagerNeutralResourcesNotFound(BaseNameField, MainAssembly, resourceFileName);
302  }
303  }
304  }
305 
311  [MethodImpl(MethodImplOptions.NoInlining)]
312  [__DynamicallyInvokable]
313  public ResourceManager(string baseName, Assembly assembly)
314  {
315  if (baseName == null)
316  {
317  throw new ArgumentNullException("baseName");
318  }
319  if (null == assembly)
320  {
321  throw new ArgumentNullException("assembly");
322  }
323  if (!(assembly is RuntimeAssembly))
324  {
325  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"));
326  }
327  MainAssembly = assembly;
328  BaseNameField = baseName;
329  SetAppXConfiguration();
330  CommonAssemblyInit();
331  m_callingAssembly = (RuntimeAssembly)Assembly.GetCallingAssembly();
332  if (assembly == typeof(object).Assembly && m_callingAssembly != assembly)
333  {
334  m_callingAssembly = null;
335  }
336  }
337 
346  [MethodImpl(MethodImplOptions.NoInlining)]
347  public ResourceManager(string baseName, Assembly assembly, Type usingResourceSet)
348  {
349  if (baseName == null)
350  {
351  throw new ArgumentNullException("baseName");
352  }
353  if (null == assembly)
354  {
355  throw new ArgumentNullException("assembly");
356  }
357  if (!(assembly is RuntimeAssembly))
358  {
359  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"));
360  }
361  MainAssembly = assembly;
362  BaseNameField = baseName;
363  if (usingResourceSet != null && usingResourceSet != _minResourceSet && !usingResourceSet.IsSubclassOf(_minResourceSet))
364  {
365  throw new ArgumentException(Environment.GetResourceString("Arg_ResMgrNotResSet"), "usingResourceSet");
366  }
367  _userResourceSet = usingResourceSet;
368  CommonAssemblyInit();
369  m_callingAssembly = (RuntimeAssembly)Assembly.GetCallingAssembly();
370  if (assembly == typeof(object).Assembly && m_callingAssembly != assembly)
371  {
372  m_callingAssembly = null;
373  }
374  }
375 
379  [MethodImpl(MethodImplOptions.NoInlining)]
380  [__DynamicallyInvokable]
381  public ResourceManager(Type resourceSource)
382  {
383  if (null == resourceSource)
384  {
385  throw new ArgumentNullException("resourceSource");
386  }
387  if (!(resourceSource is RuntimeType))
388  {
389  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
390  }
391  _locationInfo = resourceSource;
392  MainAssembly = _locationInfo.Assembly;
393  BaseNameField = resourceSource.Name;
394  SetAppXConfiguration();
395  CommonAssemblyInit();
396  m_callingAssembly = (RuntimeAssembly)Assembly.GetCallingAssembly();
397  if (MainAssembly == typeof(object).Assembly && m_callingAssembly != MainAssembly)
398  {
399  m_callingAssembly = null;
400  }
401  }
402 
403  [OnDeserializing]
404  private void OnDeserializing(StreamingContext ctx)
405  {
406  _resourceSets = null;
407  resourceGroveler = null;
408  _lastUsedResourceCache = null;
409  }
410 
411  [SecuritySafeCritical]
412  [OnDeserialized]
413  private void OnDeserialized(StreamingContext ctx)
414  {
415  _resourceSets = new Dictionary<string, ResourceSet>();
416  _lastUsedResourceCache = new CultureNameResourceSetPair();
417  ResourceManagerMediator mediator = new ResourceManagerMediator(this);
418  if (UseManifest)
419  {
420  resourceGroveler = new ManifestBasedResourceGroveler(mediator);
421  }
422  else
423  {
424  resourceGroveler = new FileBasedResourceGroveler(mediator);
425  }
426  if (m_callingAssembly == null)
427  {
428  m_callingAssembly = (RuntimeAssembly)_callingAssembly;
429  }
430  if (UseManifest && _neutralResourcesCulture == null)
431  {
432  _neutralResourcesCulture = ManifestBasedResourceGroveler.GetNeutralResourcesLanguage(MainAssembly, ref _fallbackLoc);
433  }
434  }
435 
436  [OnSerializing]
437  private void OnSerializing(StreamingContext ctx)
438  {
439  _callingAssembly = m_callingAssembly;
440  UseSatelliteAssem = UseManifest;
441  ResourceSets = new Hashtable();
442  }
443 
444  [SecuritySafeCritical]
445  private void CommonAssemblyInit()
446  {
447  if (!_bUsingModernResourceManagement)
448  {
449  UseManifest = true;
450  _resourceSets = new Dictionary<string, ResourceSet>();
451  _lastUsedResourceCache = new CultureNameResourceSetPair();
452  _fallbackLoc = UltimateResourceFallbackLocation.MainAssembly;
453  ResourceManagerMediator mediator = new ResourceManagerMediator(this);
454  resourceGroveler = new ManifestBasedResourceGroveler(mediator);
455  }
456  _neutralResourcesCulture = ManifestBasedResourceGroveler.GetNeutralResourcesLanguage(MainAssembly, ref _fallbackLoc);
457  if (_bUsingModernResourceManagement)
458  {
459  return;
460  }
461  if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled())
462  {
463  CultureInfo invariantCulture = CultureInfo.InvariantCulture;
464  string resourceFileName = GetResourceFileName(invariantCulture);
465  if (resourceGroveler.HasNeutralResources(invariantCulture, resourceFileName))
466  {
467  FrameworkEventSource.Log.ResourceManagerNeutralResourcesFound(BaseNameField, MainAssembly, resourceFileName);
468  }
469  else
470  {
471  string resName = resourceFileName;
472  if (_locationInfo != null && _locationInfo.Namespace != null)
473  {
474  resName = _locationInfo.Namespace + Type.Delimiter.ToString() + resourceFileName;
475  }
476  FrameworkEventSource.Log.ResourceManagerNeutralResourcesNotFound(BaseNameField, MainAssembly, resName);
477  }
478  }
479  ResourceSets = new Hashtable();
480  }
481 
483  public virtual void ReleaseAllResources()
484  {
485  if (FrameworkEventSource.IsInitialized)
486  {
487  FrameworkEventSource.Log.ResourceManagerReleasingResources(BaseNameField, MainAssembly);
488  }
489  Dictionary<string, ResourceSet> resourceSets = _resourceSets;
490  _resourceSets = new Dictionary<string, ResourceSet>();
491  _lastUsedResourceCache = new CultureNameResourceSetPair();
492  lock (resourceSets)
493  {
494  IDictionaryEnumerator dictionaryEnumerator = resourceSets.GetEnumerator();
495  IDictionaryEnumerator dictionaryEnumerator2 = null;
496  if (ResourceSets != null)
497  {
498  dictionaryEnumerator2 = ResourceSets.GetEnumerator();
499  }
500  ResourceSets = new Hashtable();
501  while (dictionaryEnumerator.MoveNext())
502  {
503  ((ResourceSet)dictionaryEnumerator.Value).Close();
504  }
505  if (dictionaryEnumerator2 != null)
506  {
507  while (dictionaryEnumerator2.MoveNext())
508  {
509  ((ResourceSet)dictionaryEnumerator2.Value).Close();
510  }
511  }
512  }
513  }
514 
521  public static ResourceManager CreateFileBasedResourceManager(string baseName, string resourceDir, Type usingResourceSet)
522  {
523  return new ResourceManager(baseName, resourceDir, usingResourceSet);
524  }
525 
529  protected virtual string GetResourceFileName(CultureInfo culture)
530  {
531  StringBuilder stringBuilder = new StringBuilder(255);
532  stringBuilder.Append(BaseNameField);
533  if (!culture.HasInvariantCultureName)
534  {
535  CultureInfo.VerifyCultureName(culture.Name, throwException: true);
536  stringBuilder.Append('.');
537  stringBuilder.Append(culture.Name);
538  }
539  stringBuilder.Append(".resources");
540  return stringBuilder.ToString();
541  }
542 
543  internal ResourceSet GetFirstResourceSet(CultureInfo culture)
544  {
545  if (_neutralResourcesCulture != null && culture.Name == _neutralResourcesCulture.Name)
546  {
547  culture = CultureInfo.InvariantCulture;
548  }
549  if (_lastUsedResourceCache != null)
550  {
551  lock (_lastUsedResourceCache)
552  {
553  if (culture.Name == _lastUsedResourceCache.lastCultureName)
554  {
555  return _lastUsedResourceCache.lastResourceSet;
556  }
557  }
558  }
559  Dictionary<string, ResourceSet> resourceSets = _resourceSets;
560  ResourceSet value = null;
561  if (resourceSets != null)
562  {
563  lock (resourceSets)
564  {
565  resourceSets.TryGetValue(culture.Name, out value);
566  }
567  }
568  if (value != null)
569  {
570  if (_lastUsedResourceCache != null)
571  {
572  lock (_lastUsedResourceCache)
573  {
574  _lastUsedResourceCache.lastCultureName = culture.Name;
575  _lastUsedResourceCache.lastResourceSet = value;
576  return value;
577  }
578  }
579  return value;
580  }
581  return null;
582  }
583 
594  [MethodImpl(MethodImplOptions.NoInlining)]
595  [SecuritySafeCritical]
596  public virtual ResourceSet GetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
597  {
598  if (culture == null)
599  {
600  throw new ArgumentNullException("culture");
601  }
602  Dictionary<string, ResourceSet> resourceSets = _resourceSets;
603  if (resourceSets != null)
604  {
605  lock (resourceSets)
606  {
607  if (resourceSets.TryGetValue(culture.Name, out ResourceSet value))
608  {
609  return value;
610  }
611  }
612  }
613  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
614  if (UseManifest && culture.HasInvariantCultureName)
615  {
616  string resourceFileName = GetResourceFileName(culture);
617  RuntimeAssembly runtimeAssembly = (RuntimeAssembly)MainAssembly;
618  Stream manifestResourceStream = runtimeAssembly.GetManifestResourceStream(_locationInfo, resourceFileName, m_callingAssembly == MainAssembly, ref stackMark);
619  if (createIfNotExists && manifestResourceStream != null)
620  {
621  ResourceSet value = ((ManifestBasedResourceGroveler)resourceGroveler).CreateResourceSet(manifestResourceStream, MainAssembly);
622  AddResourceSet(resourceSets, culture.Name, ref value);
623  return value;
624  }
625  }
626  return InternalGetResourceSet(culture, createIfNotExists, tryParents);
627  }
628 
639  [MethodImpl(MethodImplOptions.NoInlining)]
640  [SecuritySafeCritical]
641  protected virtual ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
642  {
643  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
644  return InternalGetResourceSet(culture, createIfNotExists, tryParents, ref stackMark);
645  }
646 
647  [SecurityCritical]
648  private ResourceSet InternalGetResourceSet(CultureInfo requestedCulture, bool createIfNotExists, bool tryParents, ref StackCrawlMark stackMark)
649  {
650  Dictionary<string, ResourceSet> resourceSets = _resourceSets;
651  ResourceSet value = null;
652  CultureInfo cultureInfo = null;
653  lock (resourceSets)
654  {
655  if (resourceSets.TryGetValue(requestedCulture.Name, out value))
656  {
657  if (FrameworkEventSource.IsInitialized)
658  {
659  FrameworkEventSource.Log.ResourceManagerFoundResourceSetInCache(BaseNameField, MainAssembly, requestedCulture.Name);
660  }
661  return value;
662  }
663  }
664  ResourceFallbackManager resourceFallbackManager = new ResourceFallbackManager(requestedCulture, _neutralResourcesCulture, tryParents);
665  foreach (CultureInfo item in resourceFallbackManager)
666  {
667  if (FrameworkEventSource.IsInitialized)
668  {
669  FrameworkEventSource.Log.ResourceManagerLookingForResourceSet(BaseNameField, MainAssembly, item.Name);
670  }
671  lock (resourceSets)
672  {
673  if (resourceSets.TryGetValue(item.Name, out value))
674  {
675  if (FrameworkEventSource.IsInitialized)
676  {
677  FrameworkEventSource.Log.ResourceManagerFoundResourceSetInCache(BaseNameField, MainAssembly, item.Name);
678  }
679  if (requestedCulture != item)
680  {
681  cultureInfo = item;
682  }
683  goto IL_013c;
684  }
685  }
686  value = resourceGroveler.GrovelForResourceSet(item, resourceSets, tryParents, createIfNotExists, ref stackMark);
687  if (value != null)
688  {
689  cultureInfo = item;
690  break;
691  }
692  }
693  goto IL_013c;
694  IL_013c:
695  if (value != null && cultureInfo != null)
696  {
697  foreach (CultureInfo item2 in resourceFallbackManager)
698  {
699  AddResourceSet(resourceSets, item2.Name, ref value);
700  if (item2 == cultureInfo)
701  {
702  return value;
703  }
704  }
705  return value;
706  }
707  return value;
708  }
709 
710  private static void AddResourceSet(Dictionary<string, ResourceSet> localResourceSets, string cultureName, ref ResourceSet rs)
711  {
712  lock (localResourceSets)
713  {
714  if (localResourceSets.TryGetValue(cultureName, out ResourceSet value))
715  {
716  if (value != rs)
717  {
718  if (!localResourceSets.ContainsValue(rs))
719  {
720  rs.Dispose();
721  }
722  rs = value;
723  }
724  }
725  else
726  {
727  localResourceSets.Add(cultureName, rs);
728  }
729  }
730  }
731 
739  {
740  if (a == null)
741  {
742  throw new ArgumentNullException("a", Environment.GetResourceString("ArgumentNull_Assembly"));
743  }
744  string text = null;
745  if (a.ReflectionOnly)
746  {
747  foreach (CustomAttributeData customAttribute in CustomAttributeData.GetCustomAttributes(a))
748  {
749  if (customAttribute.Constructor.DeclaringType == typeof(SatelliteContractVersionAttribute))
750  {
751  text = (string)customAttribute.ConstructorArguments[0].Value;
752  break;
753  }
754  }
755  if (text == null)
756  {
757  return null;
758  }
759  }
760  else
761  {
762  object[] customAttributes = a.GetCustomAttributes(typeof(SatelliteContractVersionAttribute), inherit: false);
763  if (customAttributes.Length == 0)
764  {
765  return null;
766  }
767  text = ((SatelliteContractVersionAttribute)customAttributes[0]).Version;
768  }
769  try
770  {
771  return new Version(text);
772  }
773  catch (ArgumentOutOfRangeException innerException)
774  {
775  if (!(a == typeof(object).Assembly))
776  {
777  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidSatelliteContract_Asm_Ver", a.ToString(), text), innerException);
778  }
779  return null;
780  }
781  }
782 
786  [SecuritySafeCritical]
788  {
790  return ManifestBasedResourceGroveler.GetNeutralResourcesLanguage(a, ref fallbackLocation);
791  }
792 
793  internal static bool CompareNames(string asmTypeName1, string typeName2, AssemblyName asmName2)
794  {
795  int num = asmTypeName1.IndexOf(',');
796  if (((num == -1) ? asmTypeName1.Length : num) != typeName2.Length)
797  {
798  return false;
799  }
800  if (string.Compare(asmTypeName1, 0, typeName2, 0, typeName2.Length, StringComparison.Ordinal) != 0)
801  {
802  return false;
803  }
804  if (num == -1)
805  {
806  return true;
807  }
808  while (char.IsWhiteSpace(asmTypeName1[++num]))
809  {
810  }
811  AssemblyName assemblyName = new AssemblyName(asmTypeName1.Substring(num));
812  if (string.Compare(assemblyName.Name, asmName2.Name, StringComparison.OrdinalIgnoreCase) != 0)
813  {
814  return false;
815  }
816  if (string.Compare(assemblyName.Name, "mscorlib", StringComparison.OrdinalIgnoreCase) == 0)
817  {
818  return true;
819  }
820  if (assemblyName.CultureInfo != null && asmName2.CultureInfo != null && assemblyName.CultureInfo.LCID != asmName2.CultureInfo.LCID)
821  {
822  return false;
823  }
824  byte[] publicKeyToken = assemblyName.GetPublicKeyToken();
825  byte[] publicKeyToken2 = asmName2.GetPublicKeyToken();
826  if (publicKeyToken != null && publicKeyToken2 != null)
827  {
828  if (publicKeyToken.Length != publicKeyToken2.Length)
829  {
830  return false;
831  }
832  for (int i = 0; i < publicKeyToken.Length; i++)
833  {
834  if (publicKeyToken[i] != publicKeyToken2[i])
835  {
836  return false;
837  }
838  }
839  }
840  return true;
841  }
842 
843  [SecuritySafeCritical]
844  private string GetStringFromPRI(string stringName, string startingCulture, string neutralResourcesCulture)
845  {
846  if (stringName.Length == 0)
847  {
848  return null;
849  }
850  string text = null;
851  return _WinRTResourceManager.GetString(stringName, string.IsNullOrEmpty(startingCulture) ? null : startingCulture, string.IsNullOrEmpty(neutralResourcesCulture) ? null : neutralResourcesCulture);
852  }
853 
854  [SecurityCritical]
855  internal static WindowsRuntimeResourceManagerBase GetWinRTResourceManager()
856  {
857  Type type = Type.GetType("System.Resources.WindowsRuntimeResourceManager, System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", throwOnError: true);
858  return (WindowsRuntimeResourceManagerBase)Activator.CreateInstance(type, nonPublic: true);
859  }
860 
861  [SecuritySafeCritical]
862  private bool ShouldUseSatelliteAssemblyResourceLookupUnderAppX(RuntimeAssembly resourcesAssembly)
863  {
864  return resourcesAssembly.IsFrameworkAssembly();
865  }
866 
867  [SecuritySafeCritical]
868  private void SetAppXConfiguration()
869  {
870  bool flag = false;
871  RuntimeAssembly runtimeAssembly = (RuntimeAssembly)MainAssembly;
872  if (runtimeAssembly == null)
873  {
874  runtimeAssembly = m_callingAssembly;
875  }
876  if (!(runtimeAssembly != null) || !(runtimeAssembly != typeof(object).Assembly) || !AppDomain.IsAppXModel() || AppDomain.IsAppXNGen)
877  {
878  return;
879  }
880  s_IsAppXModel = true;
881  string text = (_locationInfo == null) ? BaseNameField : _locationInfo.FullName;
882  if (text == null)
883  {
884  text = string.Empty;
885  }
886  WindowsRuntimeResourceManagerBase windowsRuntimeResourceManagerBase = null;
887  bool flag2 = false;
888  if (AppDomain.IsAppXDesignMode())
889  {
890  windowsRuntimeResourceManagerBase = GetWinRTResourceManager();
891  try
892  {
893  flag2 = windowsRuntimeResourceManagerBase.Initialize(runtimeAssembly.Location, text, out PRIExceptionInfo _);
894  flag = !flag2;
895  }
896  catch (Exception ex)
897  {
898  flag = true;
899  if (ex.IsTransient)
900  {
901  throw;
902  }
903  }
904  }
905  if (flag)
906  {
907  return;
908  }
909  _bUsingModernResourceManagement = !ShouldUseSatelliteAssemblyResourceLookupUnderAppX(runtimeAssembly);
910  if (_bUsingModernResourceManagement)
911  {
912  if (windowsRuntimeResourceManagerBase != null && flag2)
913  {
914  _WinRTResourceManager = windowsRuntimeResourceManagerBase;
915  _PRIonAppXInitialized = true;
916  }
917  else
918  {
919  _WinRTResourceManager = GetWinRTResourceManager();
920  try
921  {
922  _PRIonAppXInitialized = _WinRTResourceManager.Initialize(runtimeAssembly.Location, text, out _PRIExceptionInfo);
923  }
924  catch (FileNotFoundException)
925  {
926  }
927  catch (Exception ex3)
928  {
929  if (ex3.HResult != -2147009761)
930  {
931  throw;
932  }
933  }
934  }
935  }
936  }
937 
945  [__DynamicallyInvokable]
946  public virtual string GetString(string name)
947  {
948  return GetString(name, null);
949  }
950 
959  [__DynamicallyInvokable]
960  public virtual string GetString(string name, CultureInfo culture)
961  {
962  if (name == null)
963  {
964  throw new ArgumentNullException("name");
965  }
966  if (s_IsAppXModel && culture == CultureInfo.CurrentUICulture)
967  {
968  culture = null;
969  }
970  if (_bUsingModernResourceManagement)
971  {
972  if (!_PRIonAppXInitialized)
973  {
974  if (_PRIExceptionInfo != null && _PRIExceptionInfo._PackageSimpleName != null && _PRIExceptionInfo._ResWFile != null)
975  {
976  throw new MissingManifestResourceException(Environment.GetResourceString("MissingManifestResource_ResWFileNotLoaded", _PRIExceptionInfo._ResWFile, _PRIExceptionInfo._PackageSimpleName));
977  }
978  throw new MissingManifestResourceException(Environment.GetResourceString("MissingManifestResource_NoPRIresources"));
979  }
980  return GetStringFromPRI(name, culture?.Name, _neutralResourcesCulture.Name);
981  }
982  if (culture == null)
983  {
984  culture = Thread.CurrentThread.GetCurrentUICultureNoAppX();
985  }
986  if (FrameworkEventSource.IsInitialized)
987  {
988  FrameworkEventSource.Log.ResourceManagerLookupStarted(BaseNameField, MainAssembly, culture.Name);
989  }
990  ResourceSet resourceSet = GetFirstResourceSet(culture);
991  if (resourceSet != null)
992  {
993  string @string = resourceSet.GetString(name, _ignoreCase);
994  if (@string != null)
995  {
996  return @string;
997  }
998  }
999  ResourceFallbackManager resourceFallbackManager = new ResourceFallbackManager(culture, _neutralResourcesCulture, useParents: true);
1000  foreach (CultureInfo item in resourceFallbackManager)
1001  {
1002  ResourceSet resourceSet2 = InternalGetResourceSet(item, createIfNotExists: true, tryParents: true);
1003  if (resourceSet2 == null)
1004  {
1005  break;
1006  }
1007  if (resourceSet2 != resourceSet)
1008  {
1009  string string2 = resourceSet2.GetString(name, _ignoreCase);
1010  if (string2 != null)
1011  {
1012  if (_lastUsedResourceCache != null)
1013  {
1014  lock (_lastUsedResourceCache)
1015  {
1016  _lastUsedResourceCache.lastCultureName = item.Name;
1017  _lastUsedResourceCache.lastResourceSet = resourceSet2;
1018  }
1019  }
1020  return string2;
1021  }
1022  resourceSet = resourceSet2;
1023  }
1024  }
1025  if (FrameworkEventSource.IsInitialized)
1026  {
1027  FrameworkEventSource.Log.ResourceManagerLookupFailed(BaseNameField, MainAssembly, culture.Name);
1028  }
1029  return null;
1030  }
1031 
1038  public virtual object GetObject(string name)
1039  {
1040  return GetObject(name, null, wrapUnmanagedMemStream: true);
1041  }
1042 
1050  public virtual object GetObject(string name, CultureInfo culture)
1051  {
1052  return GetObject(name, culture, wrapUnmanagedMemStream: true);
1053  }
1054 
1055  private object GetObject(string name, CultureInfo culture, bool wrapUnmanagedMemStream)
1056  {
1057  if (name == null)
1058  {
1059  throw new ArgumentNullException("name");
1060  }
1061  if (s_IsAppXModel && culture == CultureInfo.CurrentUICulture)
1062  {
1063  culture = null;
1064  }
1065  if (culture == null)
1066  {
1067  culture = Thread.CurrentThread.GetCurrentUICultureNoAppX();
1068  }
1069  if (FrameworkEventSource.IsInitialized)
1070  {
1071  FrameworkEventSource.Log.ResourceManagerLookupStarted(BaseNameField, MainAssembly, culture.Name);
1072  }
1073  ResourceSet resourceSet = GetFirstResourceSet(culture);
1074  if (resourceSet != null)
1075  {
1076  object @object = resourceSet.GetObject(name, _ignoreCase);
1077  if (@object != null)
1078  {
1079  UnmanagedMemoryStream unmanagedMemoryStream = @object as UnmanagedMemoryStream;
1080  if (unmanagedMemoryStream != null && wrapUnmanagedMemStream)
1081  {
1082  return new UnmanagedMemoryStreamWrapper(unmanagedMemoryStream);
1083  }
1084  return @object;
1085  }
1086  }
1087  ResourceFallbackManager resourceFallbackManager = new ResourceFallbackManager(culture, _neutralResourcesCulture, useParents: true);
1088  foreach (CultureInfo item in resourceFallbackManager)
1089  {
1090  ResourceSet resourceSet2 = InternalGetResourceSet(item, createIfNotExists: true, tryParents: true);
1091  if (resourceSet2 == null)
1092  {
1093  break;
1094  }
1095  if (resourceSet2 != resourceSet)
1096  {
1097  object object2 = resourceSet2.GetObject(name, _ignoreCase);
1098  if (object2 != null)
1099  {
1100  if (_lastUsedResourceCache != null)
1101  {
1102  lock (_lastUsedResourceCache)
1103  {
1104  _lastUsedResourceCache.lastCultureName = item.Name;
1105  _lastUsedResourceCache.lastResourceSet = resourceSet2;
1106  }
1107  }
1108  UnmanagedMemoryStream unmanagedMemoryStream2 = object2 as UnmanagedMemoryStream;
1109  if (unmanagedMemoryStream2 != null && wrapUnmanagedMemStream)
1110  {
1111  return new UnmanagedMemoryStreamWrapper(unmanagedMemoryStream2);
1112  }
1113  return object2;
1114  }
1115  resourceSet = resourceSet2;
1116  }
1117  }
1118  if (FrameworkEventSource.IsInitialized)
1119  {
1120  FrameworkEventSource.Log.ResourceManagerLookupFailed(BaseNameField, MainAssembly, culture.Name);
1121  }
1122  return null;
1123  }
1124 
1133  [ComVisible(false)]
1134  public UnmanagedMemoryStream GetStream(string name)
1135  {
1136  return GetStream(name, null);
1137  }
1138 
1148  [ComVisible(false)]
1149  public UnmanagedMemoryStream GetStream(string name, CultureInfo culture)
1150  {
1151  object @object = GetObject(name, culture, wrapUnmanagedMemStream: false);
1152  UnmanagedMemoryStream unmanagedMemoryStream = @object as UnmanagedMemoryStream;
1153  if (unmanagedMemoryStream == null && @object != null)
1154  {
1155  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ResourceNotStream_Name", name));
1156  }
1157  return unmanagedMemoryStream;
1158  }
1159 
1160  [SecurityCritical]
1161  private bool TryLookingForSatellite(CultureInfo lookForCulture)
1162  {
1163  if (!_checkedConfigFile)
1164  {
1165  lock (this)
1166  {
1167  if (!_checkedConfigFile)
1168  {
1169  _checkedConfigFile = true;
1170  _installedSatelliteInfo = GetSatelliteAssembliesFromConfig();
1171  }
1172  }
1173  }
1174  if (_installedSatelliteInfo == null)
1175  {
1176  return true;
1177  }
1178  string[] array = (string[])_installedSatelliteInfo[MainAssembly.FullName];
1179  if (array == null)
1180  {
1181  return true;
1182  }
1183  int num = Array.IndexOf(array, lookForCulture.Name);
1184  if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled())
1185  {
1186  if (num < 0)
1187  {
1188  FrameworkEventSource.Log.ResourceManagerCultureNotFoundInConfigFile(BaseNameField, MainAssembly, lookForCulture.Name);
1189  }
1190  else
1191  {
1192  FrameworkEventSource.Log.ResourceManagerCultureFoundInConfigFile(BaseNameField, MainAssembly, lookForCulture.Name);
1193  }
1194  }
1195  return num >= 0;
1196  }
1197 
1198  [SecurityCritical]
1199  private Hashtable GetSatelliteAssembliesFromConfig()
1200  {
1201  string configurationFileInternal = AppDomain.CurrentDomain.FusionStore.ConfigurationFileInternal;
1202  if (configurationFileInternal == null)
1203  {
1204  return null;
1205  }
1206  if (configurationFileInternal.Length >= 2 && (configurationFileInternal[1] == Path.VolumeSeparatorChar || (configurationFileInternal[0] == Path.DirectorySeparatorChar && configurationFileInternal[1] == Path.DirectorySeparatorChar)) && !File.InternalExists(configurationFileInternal))
1207  {
1208  return null;
1209  }
1210  ConfigTreeParser configTreeParser = new ConfigTreeParser();
1211  string configPath = "/configuration/satelliteassemblies";
1212  ConfigNode configNode = null;
1213  try
1214  {
1215  configNode = configTreeParser.Parse(configurationFileInternal, configPath, skipSecurityStuff: true);
1216  }
1217  catch (Exception)
1218  {
1219  }
1220  if (configNode == null)
1221  {
1222  return null;
1223  }
1224  Hashtable hashtable = new Hashtable(StringComparer.OrdinalIgnoreCase);
1225  foreach (ConfigNode child in configNode.Children)
1226  {
1227  if (!string.Equals(child.Name, "assembly"))
1228  {
1229  throw new ApplicationException(Environment.GetResourceString("XMLSyntax_InvalidSyntaxSatAssemTag", Path.GetFileName(configurationFileInternal), child.Name));
1230  }
1231  if (child.Attributes.Count == 0)
1232  {
1233  throw new ApplicationException(Environment.GetResourceString("XMLSyntax_InvalidSyntaxSatAssemTagNoAttr", Path.GetFileName(configurationFileInternal)));
1234  }
1235  DictionaryEntry dictionaryEntry = child.Attributes[0];
1236  string text = (string)dictionaryEntry.Value;
1237  if (!object.Equals(dictionaryEntry.Key, "name") || string.IsNullOrEmpty(text) || child.Attributes.Count > 1)
1238  {
1239  throw new ApplicationException(Environment.GetResourceString("XMLSyntax_InvalidSyntaxSatAssemTagBadAttr", Path.GetFileName(configurationFileInternal), dictionaryEntry.Key, dictionaryEntry.Value));
1240  }
1241  ArrayList arrayList = new ArrayList(5);
1242  foreach (ConfigNode child2 in child.Children)
1243  {
1244  if (child2.Value != null)
1245  {
1246  arrayList.Add(child2.Value);
1247  }
1248  }
1249  string[] array = new string[arrayList.Count];
1250  for (int i = 0; i < array.Length; i++)
1251  {
1252  string cultureName = array[i] = (string)arrayList[i];
1253  if (FrameworkEventSource.IsInitialized)
1254  {
1255  FrameworkEventSource.Log.ResourceManagerAddingCultureFromConfigFile(BaseNameField, MainAssembly, cultureName);
1256  }
1257  }
1258  hashtable.Add(text, array);
1259  }
1260  return hashtable;
1261  }
1262  }
1263 }
static Thread CurrentThread
Gets the currently running thread.
Definition: Thread.cs:134
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
string BaseNameField
Specifies the root name of the resource files that the T:System.Resources.ResourceManager searches fo...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
bool MoveNext()
Advances the enumerator to the next element of the collection.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
virtual void ReleaseAllResources()
Tells the resource manager to call the M:System.Resources.ResourceSet.Close method on all T:System....
abstract string FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
Definition: Type.cs:153
virtual string FullName
Gets the display name of the assembly.
Definition: Assembly.cs:49
static Assembly GetCallingAssembly()
Returns the T:System.Reflection.Assembly of the method that invoked the currently executing method.
Definition: Assembly.cs:810
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
virtual string GetString(string name, CultureInfo culture)
Returns the value of the string resource localized for the specified culture.
bool ContainsValue(TValue value)
Determines whether the T:System.Collections.Generic.Dictionary`2 contains a specific value.
Definition: Dictionary.cs:1313
virtual ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
Provides the implementation for finding a resource set.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
ResourceManager(string baseName, Assembly assembly)
Initializes a new instance of the T:System.Resources.ResourceManager class that looks up resources co...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Provides access to custom attribute data for assemblies, modules, types, members and parameters that ...
virtual string GetResourceFileName(CultureInfo culture)
Generates the name of the resource file for the given T:System.Globalization.CultureInfo object.
virtual string Name
Gets the culture name in the format languagecode2-country/regioncode2.
Definition: CultureInfo.cs:321
static IList< CustomAttributeData > GetCustomAttributes(MemberInfo target)
Returns a list of T:System.Reflection.CustomAttributeData objects representing data about the attribu...
Instructs a T:System.Resources.ResourceManager object to ask for a particular version of a satellite ...
static string GetFileName(string path)
Returns the file name and extension of the specified path string.
Definition: Path.cs:914
static readonly char DirectorySeparatorChar
Provides a platform-specific character used to separate directory levels in a path string that reflec...
Definition: Path.cs:17
Describes the source and destination of a given serialized stream, and provides an additional caller-...
abstract string Namespace
Gets the namespace of the T:System.Type.
Definition: Type.cs:162
Represents a resource manager that provides convenient access to culture-specific resources at run ti...
virtual object [] GetCustomAttributes(bool inherit)
Gets all the custom attributes for this assembly.
Definition: Assembly.cs:1041
virtual object GetObject(string name, CultureInfo culture)
Gets the value of the specified non-string resource localized for the specified culture.
static CultureInfo CurrentUICulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the current user interface...
Definition: CultureInfo.cs:174
virtual bool IsSubclassOf(Type c)
Determines whether the current T:System.Type derives from the specified T:System.Type.
Definition: Type.cs:2664
override string ToString()
Returns the full name of the assembly, also known as the display name.
Definition: Assembly.cs:1306
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
static ResourceManager CreateFileBasedResourceManager(string baseName, string resourceDir, Type usingResourceSet)
Returns a T:System.Resources.ResourceManager object that searches a specific directory instead of an ...
string Name
Gets or sets the simple name of the assembly. This is usually, but not necessarily,...
Definition: AssemblyName.cs:51
UltimateResourceFallbackLocation FallbackLocation
Gets or sets the location from which to retrieve default fallback resources.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
static readonly int HeaderVersionNumber
Specifies the version of resource file headers that the current implementation of T:System....
CultureInfo CultureInfo
Gets or sets the culture supported by the assembly.
Definition: AssemblyName.cs:85
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
virtual Type ResourceSetType
Gets the type of the resource set object that the resource manager uses to construct a T:System....
virtual bool IgnoreCase
Gets or sets a value that indicates whether the resource manager allows case-insensitive resource loo...
virtual string GetString(string name)
Returns the value of the specified string resource.
abstract Assembly Assembly
Gets the T:System.Reflection.Assembly in which the type is declared. For generic types,...
Definition: Type.cs:131
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
UnmanagedMemoryStream GetStream(string name, CultureInfo culture)
Returns an unmanaged memory stream object from the specified resource, using the specified culture.
MethodImplOptions
Defines the details of how a method is implemented.
abstract string Name
Gets the name of the current member.
Definition: MemberInfo.cs:27
Enumerator GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.Generic.Dictionary`2.
Definition: Dictionary.cs:1367
UltimateResourceFallbackLocation
Specifies whether a T:System.Resources.ResourceManager object looks for the resources of the app's de...
Enumerates the resources in a binary resources (.resources) file by reading sequential resource name/...
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
The exception that is thrown if the main assembly does not contain the resources for the neutral cult...
Represents a collection of keys and values.To browse the .NET Framework source code for this type,...
Definition: Dictionary.cs:17
Describes an assembly's unique identity in full.
Definition: AssemblyName.cs:19
Type DeclaringType
Provides COM objects with version-independent access to the P:System.Reflection.MemberInfo....
The exception that is thrown when one of the arguments provided to a method is not valid.
static Version GetSatelliteContractVersion(Assembly a)
Returns the version specified by the T:System.Resources.SatelliteContractVersionAttribute attribute i...
The exception that is thrown when an attempt to access a file that does not exist on disk fails.
virtual string GetString(string name)
Searches for a T:System.String resource with the specified name.
Definition: ResourceSet.cs:147
virtual ConstructorInfo Constructor
Gets a T:System.Reflection.ConstructorInfo object that represents the constructor that would have ini...
virtual object GetObject(string name)
Returns the value of the specified non-string resource.
object Value
Gets or sets the value in the key/value pair.
object Value
Gets the value of the current dictionary entry.
ResourceManager()
Initializes a new instance of the T:System.Resources.ResourceManager class with default values.
static readonly int MagicNumber
Holds the number used to identify resource files.
Specifies that the class can be serialized.
Stores all the resources localized for one particular culture, ignoring all other cultures,...
Definition: ResourceSet.cs:12
Enumerates the elements of a nongeneric dictionary.
virtual bool ReflectionOnly
Gets a T:System.Boolean value indicating whether this assembly was loaded into the reflection-only co...
Definition: Assembly.cs:182
The exception that is thrown when a method call is invalid for the object's current state.
Provides static methods for the creation, copying, deletion, moving, and opening of a single file,...
Definition: File.cs:14
virtual int LCID
Gets the culture identifier for the current T:System.Globalization.CultureInfo.
Definition: CultureInfo.cs:304
void Add(TKey key, TValue value)
Adds the specified key and value to the dictionary.
Definition: Dictionary.cs:1244
object Key
Gets or sets the key in the key/value pair.
virtual string BaseName
Gets the root name of the resource files that the T:System.Resources.ResourceManager searches for res...
Provides access to unmanaged blocks of memory from managed code.
UnmanagedMemoryStream GetStream(string name)
Returns an unmanaged memory stream object from the specified resource.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
virtual IList< CustomAttributeTypedArgument > ConstructorArguments
Gets the list of positional arguments specified for the attribute instance represented by the T:Syste...
static CultureInfo GetNeutralResourcesLanguage(Assembly a)
Returns culture-specific information for the main assembly's default resources by retrieving the valu...
virtual ResourceSet GetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
Retrieves the resource set for a particular culture.
ResourceManager(Type resourceSource)
Initializes a new instance of the T:System.Resources.ResourceManager class that looks up resources in...
Defines a dictionary key/value pair that can be set or retrieved.
Hashtable ResourceSets
Contains a T:System.Collections.Hashtable that returns a mapping from cultures to T:System....
Assembly MainAssembly
Specifies the main assembly that contains the resources.
bool TryGetValue(TKey key, out TValue value)
Gets the value associated with the specified key.
Definition: Dictionary.cs:1624
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
byte [] GetPublicKeyToken()
Gets the public key token, which is the last 8 bytes of the SHA-1 hash of the public key under which ...
ResourceManager(string baseName, Assembly assembly, Type usingResourceSet)
Initializes a new instance of the T:System.Resources.ResourceManager class that uses a specified T:Sy...
static readonly char VolumeSeparatorChar
Provides a platform-specific volume separator character.
Definition: Path.cs:27
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18