diff options
author | Jan Kotas <jkotas@microsoft.com> | 2019-02-02 09:14:47 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-02 09:14:47 -0800 |
commit | c3c07ece61dc18454d0aad985dc7004b9ce63c26 (patch) | |
tree | 5669ee8d3f863358151687f4787846f328f33c57 | |
parent | 0f8b7e89ca98134e393369671bc902834a2a6fe9 (diff) | |
download | coreclr-c3c07ece61dc18454d0aad985dc7004b9ce63c26.tar.gz coreclr-c3c07ece61dc18454d0aad985dc7004b9ce63c26.tar.bz2 coreclr-c3c07ece61dc18454d0aad985dc7004b9ce63c26.zip |
Rename ResourceManager fields to follow conventions (#22371)
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Resources/ResourceManager.Uap.cs | 228 | ||||
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Resources/ResourceManager.cs | 36 |
2 files changed, 119 insertions, 145 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.Uap.cs b/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.Uap.cs index 8a89f5e757..1faf55f4b4 100644 --- a/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.Uap.cs +++ b/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.Uap.cs @@ -24,20 +24,19 @@ namespace System.Resources public partial class ResourceManager { private WindowsRuntimeResourceManagerBase _WinRTResourceManager; - private bool _PRIonAppXInitialized; private PRIExceptionInfo _PRIExceptionInfo; - - private bool UseUapResourceManagement { get; set; } + private bool _PRIInitialized; + private bool _useUapResourceManagement; private string GetStringFromPRI(string stringName, CultureInfo culture, string neutralResourcesCulture) { - Debug.Assert(UseUapResourceManagement); + Debug.Assert(_useUapResourceManagement); Debug.Assert(_WinRTResourceManager != null); - Debug.Assert(_PRIonAppXInitialized); + Debug.Assert(_PRIInitialized); // If the caller explicitly passed in a culture that was obtained by calling CultureInfo.CurrentUICulture, // null it out, so that we re-compute it. If we use modern resource lookup, we may end up getting a "better" - // match, since CultureInfo objects can't represent all the different languages the AppX resource model supports. + // match, since CultureInfo objects can't represent all the different languages the Uap resource model supports. if (object.ReferenceEquals(culture, CultureInfo.CurrentUICulture)) { culture = null; @@ -45,7 +44,7 @@ namespace System.Resources string startingCulture = culture?.Name; - if (_PRIonAppXInitialized == false) + if (_PRIInitialized == false) { // Always throw if we did not fully succeed in initializing the WinRT Resource Manager. @@ -58,36 +57,27 @@ namespace System.Resources if (stringName.Length == 0) return null; - string resourceString = null; - - // Do not handle exceptions. See the comment in SetAppXConfiguration about throwing + // Do not handle exceptions. See the comment in SetUapConfiguration about throwing // exception types that the ResourceManager class is not documented to throw. - resourceString = _WinRTResourceManager.GetString( + return _WinRTResourceManager.GetString( stringName, string.IsNullOrEmpty(startingCulture) ? null : startingCulture, string.IsNullOrEmpty(neutralResourcesCulture) ? null : neutralResourcesCulture); - - return resourceString; } // Since we can't directly reference System.Runtime.WindowsRuntime from System.Private.CoreLib, we have to get the type via reflection. // It would be better if we could just implement WindowsRuntimeResourceManager in System.Private.CoreLib, but we can't, because // we can do very little with WinRT in System.Private.CoreLib. -#if FEATURE_APPX internal static WindowsRuntimeResourceManagerBase GetWinRTResourceManager() { +#if FEATURE_APPX Type WinRTResourceManagerType = Type.GetType("System.Resources.WindowsRuntimeResourceManager, System.Runtime.WindowsRuntime", throwOnError: true); - return (WindowsRuntimeResourceManagerBase)Activator.CreateInstance(WinRTResourceManagerType, true); - } -#endif -#if ENABLE_WINRT - internal static WindowsRuntimeResourceManagerBase GetWinRTResourceManager() - { +#else // ENABLE_WINRT Assembly hiddenScopeAssembly = Assembly.Load(RuntimeAugments.HiddenScopeAssemblyName); Type WinRTResourceManagerType = hiddenScopeAssembly.GetType("System.Resources.WindowsRuntimeResourceManager", true); +#endif return (WindowsRuntimeResourceManagerBase)Activator.CreateInstance(WinRTResourceManagerType, true); } -#endif // CoreCLR: When running under AppX, the following rules apply for resource lookup: // @@ -103,10 +93,10 @@ namespace System.Resources // .NET Native: If it is framework assembly we'll return true. The reason is in .NetNative we don't merge the // resources to the app PRI file. // The framework assemblies are tagged with attribute [assembly: AssemblyMetadata(".NETFrameworkAssembly", "")] - private bool ShouldUseSatelliteAssemblyResourceLookupUnderAppX(Assembly resourcesAssembly) + private static bool ShouldUseUapResourceManagement(Assembly resourcesAssembly) { - if (typeof(object).Assembly == resourcesAssembly) - return true; + if (resourcesAssembly == typeof(object).Assembly) // We are not loading resources for System.Private.CoreLib + return false; #if FEATURE_APPX // Check to see if the assembly is under PLATFORM_RESOURCE_ROOTS. If it is, then we should use satellite assembly lookup for it. @@ -121,7 +111,7 @@ namespace System.Resources if (resourceAssemblyPath.StartsWith(pathPlatformResourceRoot, StringComparison.CurrentCultureIgnoreCase)) { // Found the resource assembly to be present in one of the PLATFORM_RESOURCE_ROOT, so stop the enumeration loop. - return true; + return false; } } } @@ -131,126 +121,110 @@ namespace System.Resources AssemblyMetadataAttribute meta = attrib as AssemblyMetadataAttribute; if (meta != null && meta.Key.Equals(".NETFrameworkAssembly")) { - return true; + return false; } } #endif - return false; + return true; } - // Only call SetAppXConfiguration from ResourceManager constructors, and nowhere else. + // Only call SetUapConfiguration from ResourceManager constructors, and nowhere else. // Throws MissingManifestResourceException and WinRT HResults - private void SetAppXConfiguration() + private void SetUapConfiguration() { - Debug.Assert(UseUapResourceManagement == false); // Only this function writes to this member + Debug.Assert(_useUapResourceManagement == false); // Only this function writes to this member Debug.Assert(_WinRTResourceManager == null); // Only this function writes to this member - Debug.Assert(_PRIonAppXInitialized == false); // Only this function writes to this member + Debug.Assert(_PRIInitialized == false); // Only this function writes to this member Debug.Assert(_PRIExceptionInfo == null); // Only this function writes to this member - bool bUsingSatelliteAssembliesUnderAppX = false; - - if (MainAssembly != null) - { - if (MainAssembly != typeof(object).Assembly) // We are not loading resources for System.Private.CoreLib - { -#if ENABLE_WINRT - WinRTInteropCallbacks callbacks = WinRTInterop.UnsafeCallbacks; - if (callbacks != null && callbacks.IsAppxModel()) -#else - if (ApplicationModel.IsUap) +#if FEATURE_APPX + if (!ApplicationModel.IsUap) + return; +#else // ENABLE_WINRT + if (!(callbacks != null && callbacks.IsAppxModel())) + return; #endif - { - // If we have the type information from the ResourceManager(Type) constructor, we use it. Otherwise, we use BaseNameField. - string reswFilename = _locationInfo == null ? BaseNameField : _locationInfo.FullName; - - // The only way this can happen is if a class inherited from ResourceManager and - // did not set the BaseNameField before calling the protected ResourceManager() constructor. - // For other constructors, we would already have thrown an ArgumentNullException by now. - // Throwing an ArgumentNullException now is not the right thing to do because technically - // ResourceManager() takes no arguments, and because it is not documented as throwing - // any exceptions. Instead, let's go through the rest of the initialization with this set to - // an empty string. We may in fact fail earlier for another reason, but otherwise we will - // throw a MissingManifestResourceException when GetString is called indicating that a - // resW filename called "" could not be found. - if (reswFilename == null) - reswFilename = string.Empty; - - if (!bUsingSatelliteAssembliesUnderAppX) - { - UseUapResourceManagement = !ShouldUseSatelliteAssemblyResourceLookupUnderAppX(MainAssembly); - - if (UseUapResourceManagement) - { - // Only now are we certain that we need the PRI file. - - // At this point it is important NOT to set UseUapResourceManagement to false - // if the PRI file does not exist because we are now certain we need to load PRI - // resources. We want to fail by throwing a MissingManifestResourceException - // if WindowsRuntimeResourceManager.Initialize fails to locate the PRI file. We do not - // want to fall back to using satellite assemblies anymore. Note that we would not throw - // the MissingManifestResourceException from this function, but from GetString. See the - // comment below on the reason for this. - _WinRTResourceManager = GetWinRTResourceManager(); - - try - { - _PRIonAppXInitialized = _WinRTResourceManager.Initialize(MainAssembly.Location, reswFilename, out _PRIExceptionInfo); - // Note that _PRIExceptionInfo might be null - this is OK. - // In that case we will just throw the generic - // MissingManifestResource_NoPRIresources exception. - // See the implementation of GetString for more details. - } - // We would like to be able to throw a MissingManifestResourceException here if PRI resources - // could not be loaded for a recognized reason. However, the ResourceManager constructors - // that call SetAppXConfiguration are not documented as throwing MissingManifestResourceException, - // and since they are part of the portable profile, we cannot start throwing a new exception type - // as that would break existing portable libraries. Hence we must save the exception information - // now and throw the exception on the first call to GetString. - catch (FileNotFoundException) - { - // We will throw MissingManifestResource_NoPRIresources from GetString - // when we see that _PRIonAppXInitialized is false. - } - catch (Exception e) - { - // ERROR_MRM_MAP_NOT_FOUND can be thrown by the call to ResourceManager.get_AllResourceMaps - // in WindowsRuntimeResourceManager.Initialize. - // In this case _PRIExceptionInfo is now null and we will just throw the generic - // MissingManifestResource_NoPRIresources exception. - // See the implementation of GetString for more details. - if (e.HResult != HResults.ERROR_MRM_MAP_NOT_FOUND) - throw; // Unexpected exception code. Bubble it up to the caller. - } + if (!ShouldUseUapResourceManagement(MainAssembly)) + return; + + _useUapResourceManagement = true; + + // If we have the type information from the ResourceManager(Type) constructor, we use it. Otherwise, we use BaseNameField. + string reswFilename = _locationInfo == null ? BaseNameField : _locationInfo.FullName; + + // The only way this can happen is if a class inherited from ResourceManager and + // did not set the BaseNameField before calling the protected ResourceManager() constructor. + // For other constructors, we would already have thrown an ArgumentNullException by now. + // Throwing an ArgumentNullException now is not the right thing to do because technically + // ResourceManager() takes no arguments, and because it is not documented as throwing + // any exceptions. Instead, let's go through the rest of the initialization with this set to + // an empty string. We may in fact fail earlier for another reason, but otherwise we will + // throw a MissingManifestResourceException when GetString is called indicating that a + // resW filename called "" could not be found. + if (reswFilename == null) + reswFilename = string.Empty; + + // At this point it is important NOT to set _useUapResourceManagement to false + // if the PRI file does not exist because we are now certain we need to load PRI + // resources. We want to fail by throwing a MissingManifestResourceException + // if WindowsRuntimeResourceManager.Initialize fails to locate the PRI file. We do not + // want to fall back to using satellite assemblies anymore. Note that we would not throw + // the MissingManifestResourceException from this function, but from GetString. See the + // comment below on the reason for this. + + _WinRTResourceManager = GetWinRTResourceManager(); + + try + { + _PRIInitialized = _WinRTResourceManager.Initialize(MainAssembly.Location, reswFilename, out _PRIExceptionInfo); + // Note that _PRIExceptionInfo might be null - this is OK. + // In that case we will just throw the generic + // MissingManifestResource_NoPRIresources exception. + // See the implementation of GetString for more details. + } + // We would like to be able to throw a MissingManifestResourceException here if PRI resources + // could not be loaded for a recognized reason. However, the ResourceManager constructors + // that call SetUapConfiguration are not documented as throwing MissingManifestResourceException, + // and since they are part of the portable profile, we cannot start throwing a new exception type + // as that would break existing portable libraries. Hence we must save the exception information + // now and throw the exception on the first call to GetString. + catch (FileNotFoundException) + { + // We will throw MissingManifestResource_NoPRIresources from GetString + // when we see that _PRIInitialized is false. + } + catch (Exception e) + { + // ERROR_MRM_MAP_NOT_FOUND can be thrown by the call to ResourceManager.get_AllResourceMaps + // in WindowsRuntimeResourceManager.Initialize. + // In this case _PRIExceptionInfo is now null and we will just throw the generic + // MissingManifestResource_NoPRIresources exception. + // See the implementation of GetString for more details. + if (e.HResult != HResults.ERROR_MRM_MAP_NOT_FOUND) + throw; // Unexpected exception code. Bubble it up to the caller. + } - if (!_PRIonAppXInitialized) - { - UseUapResourceManagement = false; - } - // Allow all other exception types to bubble up to the caller. + if (!_PRIInitialized) + { + _useUapResourceManagement = false; + } - // Yes, this causes us to potentially throw exception types that are not documented. + // Allow all other exception types to bubble up to the caller. - // Ultimately the tradeoff is the following: - // -We could ignore unknown exceptions or rethrow them as inner exceptions - // of exceptions that the ResourceManager class is already documented as throwing. - // This would allow existing portable libraries to gracefully recover if they don't care - // too much about the ResourceManager object they are using. However it could - // mask potentially fatal errors that we are not aware of, such as a disk drive failing. + // Yes, this causes us to potentially throw exception types that are not documented. + // Ultimately the tradeoff is the following: + // -We could ignore unknown exceptions or rethrow them as inner exceptions + // of exceptions that the ResourceManager class is already documented as throwing. + // This would allow existing portable libraries to gracefully recover if they don't care + // too much about the ResourceManager object they are using. However it could + // mask potentially fatal errors that we are not aware of, such as a disk drive failing. - // The alternative, which we chose, is to throw unknown exceptions. This may tear - // down the process if the portable library and app don't expect this exception type. - // On the other hand, this won't mask potentially fatal errors we don't know about. - } - } - } - } - } - // MainAssembly == null should not happen but it can. See the comment on Assembly.GetCallingAssembly. - // However for the sake of 100% backwards compatibility on Win7 and below, we must leave - // _bUsingModernResourceManagement as false. + // The alternative, which we chose, is to throw unknown exceptions. This may tear + // down the process if the portable library and app don't expect this exception type. + // On the other hand, this won't mask potentially fatal errors we don't know about. } } } diff --git a/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.cs b/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.cs index 8de1c0bea5..f13afc1843 100644 --- a/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.cs +++ b/src/System.Private.CoreLib/shared/System/Resources/ResourceManager.cs @@ -100,19 +100,19 @@ namespace System.Resources } protected string BaseNameField; + protected Assembly MainAssembly; // Need the assembly manifest sometimes. private Dictionary<string, ResourceSet> _resourceSets; - private string moduleDir; // For assembly-ignorant directory location - protected Assembly MainAssembly; // Need the assembly manifest sometimes. - private Type _locationInfo; // For Assembly or type-based directory layout - private Type _userResourceSet; // Which ResourceSet instance to create + private string _moduleDir; // For assembly-ignorant directory location + private Type _locationInfo; // For Assembly or type-based directory layout + private Type _userResourceSet; // Which ResourceSet instance to create private CultureInfo _neutralResourcesCulture; // For perf optimizations. private CultureNameResourceSetPair _lastUsedResourceCache; private bool _ignoreCase; // Whether case matters in GetString & GetObject - private bool UseManifest; // Use Assembly manifest, or grovel disk. + private bool _useManifest; // Use Assembly manifest, or grovel disk. // Whether to fall back to the main assembly or a particular // satellite for the neutral resources. @@ -121,7 +121,7 @@ namespace System.Resources private Version _satelliteContractVersion; private bool _lookedForSatelliteContractVersion; - private IResourceGroveler resourceGroveler; + private IResourceGroveler _resourceGroveler; public static readonly int MagicNumber = unchecked((int)0xBEEFCACE); // If only hex had a K... @@ -155,7 +155,7 @@ namespace System.Resources { _lastUsedResourceCache = new CultureNameResourceSetPair(); ResourceManagerMediator mediator = new ResourceManagerMediator(this); - resourceGroveler = new ManifestBasedResourceGroveler(mediator); + _resourceGroveler = new ManifestBasedResourceGroveler(mediator); } // Constructs a Resource Manager for files beginning with @@ -177,14 +177,14 @@ namespace System.Resources BaseNameField = baseName; - moduleDir = resourceDir; + _moduleDir = resourceDir; _userResourceSet = usingResourceSet; _resourceSets = new Dictionary<string, ResourceSet>(); _lastUsedResourceCache = new CultureNameResourceSetPair(); - UseManifest = false; + _useManifest = false; ResourceManagerMediator mediator = new ResourceManagerMediator(this); - resourceGroveler = new FileBasedResourceGroveler(mediator); + _resourceGroveler = new FileBasedResourceGroveler(mediator); } public ResourceManager(string baseName, Assembly assembly) @@ -240,11 +240,11 @@ namespace System.Resources private void CommonAssemblyInit() { #if FEATURE_APPX || ENABLE_WINRT - SetAppXConfiguration(); + SetUapConfiguration(); #endif // Now we can use the managed resources even when using PRI's to support the APIs GetObject, GetStream...etc. - UseManifest = true; + _useManifest = true; _resourceSets = new Dictionary<string, ResourceSet>(); _lastUsedResourceCache = new CultureNameResourceSetPair(); @@ -252,7 +252,7 @@ namespace System.Resources _fallbackLoc = UltimateResourceFallbackLocation.MainAssembly; ResourceManagerMediator mediator = new ResourceManagerMediator(this); - resourceGroveler = new ManifestBasedResourceGroveler(mediator); + _resourceGroveler = new ManifestBasedResourceGroveler(mediator); _neutralResourcesCulture = ManifestBasedResourceGroveler.GetNeutralResourcesLanguage(MainAssembly, ref _fallbackLoc); } @@ -411,13 +411,13 @@ namespace System.Resources } } - if (UseManifest && culture.HasInvariantCultureName) + if (_useManifest && culture.HasInvariantCultureName) { string fileName = GetResourceFileName(culture); Stream stream = MainAssembly.GetManifestResourceStream(_locationInfo, fileName); if (createIfNotExists && stream != null) { - rs = ((ManifestBasedResourceGroveler)resourceGroveler).CreateResourceSet(stream, MainAssembly); + rs = ((ManifestBasedResourceGroveler)_resourceGroveler).CreateResourceSet(stream, MainAssembly); AddResourceSet(localResourceSets, culture.Name, ref rs); return rs; } @@ -465,7 +465,7 @@ namespace System.Resources // Assembly load event, which could fail then call back into the // ResourceManager). It's happened. - rs = resourceGroveler.GrovelForResourceSet(currentCultureInfo, localResourceSets, + rs = _resourceGroveler.GrovelForResourceSet(currentCultureInfo, localResourceSets, tryParents, createIfNotExists); // found a ResourceSet; we're done @@ -608,7 +608,7 @@ namespace System.Resources throw new ArgumentNullException(nameof(name)); #if FEATURE_APPX || ENABLE_WINRT - if (UseUapResourceManagement) + if (_useUapResourceManagement) { // Throws WinRT hresults. return GetStringFromPRI(name, culture, _neutralResourcesCulture.Name); @@ -773,7 +773,7 @@ namespace System.Resources // NEEDED ONLY BY FILE-BASED internal string ModuleDir { - get { return _rm.moduleDir; } + get { return _rm._moduleDir; } } // NEEDED BOTH BY FILE-BASED AND ASSEMBLY-BASED |