Delete CoreLib.FixupCoreLibName (#21655)
authorJan Kotas <jkotas@microsoft.com>
Sun, 23 Dec 2018 12:47:11 +0000 (04:47 -0800)
committerGitHub <noreply@github.com>
Sun, 23 Dec 2018 12:47:11 +0000 (04:47 -0800)
This method doing blind find&replace of "mscorlib" with "System.Private.CoreLib". It is both incorrect (e.g. if the typename happened to contain "mscorlib" string) and inefficient (unnecessary allocations and use of reflection).

src/System.Private.CoreLib/shared/System/CoreLib.cs
src/System.Private.CoreLib/shared/System/Resources/ResourceReader.cs
src/System.Private.CoreLib/src/System/Resources/ManifestBasedResourceGroveler.cs
src/System.Private.CoreLib/src/System/Resources/ResourceManager.cs

index a476038..9c5d1c1 100644 (file)
@@ -8,15 +8,5 @@ namespace System
     internal static class CoreLib
     {
         public const string Name = "System.Private.CoreLib";
-
-        public static string FixupCoreLibName(string strToFixup)
-        {
-            if (!string.IsNullOrEmpty(strToFixup))
-            {
-                strToFixup = strToFixup.Replace("mscorlib", System.CoreLib.Name);
-            }
-
-            return strToFixup;
-        }
     }
 }
index 68d4b2e..7170a98 100644 (file)
@@ -863,10 +863,8 @@ namespace System.Resources
                 // Read in type name for a suitable ResourceReader
                 // Note ResourceWriter & InternalResGen use different Strings.
                 string readerType = _store.ReadString();
-                readerType = System.CoreLib.FixupCoreLibName(readerType);
-                AssemblyName mscorlib = new AssemblyName(ResourceManager.MscorlibName);
 
-                if (!ResourceManager.CompareNames(readerType, ResourceManager.ResReaderTypeName, mscorlib))
+                if (!ResourceManager.IsDefaultType(readerType, ResourceManager.ResReaderTypeName))
                     throw new NotSupportedException(SR.Format(SR.NotSupported_WrongResourceReader_Type, readerType));
 
                 // Skip over type name for a suitable ResourceSet
index 7675f15..b4f523b 100644 (file)
@@ -206,8 +206,8 @@ namespace System.Resources
                     if (resMgrHeaderVersion == ResourceManager.HeaderVersionNumber)
                     {
                         br.ReadInt32();  // We don't want the number of bytes to skip.
-                        readerTypeName = System.CoreLib.FixupCoreLibName(br.ReadString());
-                        resSetTypeName = System.CoreLib.FixupCoreLibName(br.ReadString());
+                        readerTypeName = br.ReadString();
+                        resSetTypeName = br.ReadString();
                     }
                     else if (resMgrHeaderVersion > ResourceManager.HeaderVersionNumber)
                     {
@@ -218,8 +218,8 @@ namespace System.Resources
                         int numBytesToSkip = br.ReadInt32();
                         long endPosition = br.BaseStream.Position + numBytesToSkip;
 
-                        readerTypeName = System.CoreLib.FixupCoreLibName(br.ReadString());
-                        resSetTypeName = System.CoreLib.FixupCoreLibName(br.ReadString());
+                        readerTypeName = br.ReadString();
+                        resSetTypeName = br.ReadString();
 
                         br.BaseStream.Seek(endPosition, SeekOrigin.Begin);
                     }
@@ -242,8 +242,7 @@ namespace System.Resources
                     }
                     else
                     {
-                        // we do not want to use partial binding here.
-                        Type readerType = Type.GetType(readerTypeName, true);
+                        Type readerType = Type.GetType(readerTypeName, throwOnError: true);
                         object[] args = new object[1];
                         args[0] = store;
                         IResourceReader reader = (IResourceReader)Activator.CreateInstance(readerType, args);
@@ -439,17 +438,16 @@ namespace System.Resources
             // Ignore the actual version of the ResourceReader and 
             // RuntimeResourceSet classes.  Let those classes deal with
             // versioning themselves.
-            AssemblyName mscorlib = new AssemblyName(ResourceManager.MscorlibName);
 
             if (readerTypeName != null)
             {
-                if (!ResourceManager.CompareNames(readerTypeName, ResourceManager.ResReaderTypeName, mscorlib))
+                if (!ResourceManager.IsDefaultType(readerTypeName, ResourceManager.ResReaderTypeName))
                     return false;
             }
 
             if (resSetTypeName != null)
             {
-                if (!ResourceManager.CompareNames(resSetTypeName, ResourceManager.ResSetTypeName, mscorlib))
+                if (!ResourceManager.IsDefaultType(resSetTypeName, ResourceManager.ResSetTypeName))
                     return false;
             }
 
index 9e4f463..774636b 100644 (file)
@@ -173,13 +173,10 @@ namespace System.Resources
         //      private static CultureInfo _neutralCulture = null;
 
         // This is our min required ResourceSet type.
-        private static readonly Type _minResourceSet = typeof(ResourceSet);
+        private static readonly Type s_minResourceSet = typeof(ResourceSet);
         // These Strings are used to avoid using Reflection in CreateResourceSet.
-        // The first set are used by ResourceWriter.  The second are used by
-        // InternalResGen.
-        internal static readonly string ResReaderTypeName = typeof(ResourceReader).FullName;
-        internal static readonly string ResSetTypeName = typeof(RuntimeResourceSet).FullName;
-        internal static readonly string MscorlibName = typeof(ResourceReader).Assembly.FullName;
+        internal const string ResReaderTypeName = "System.Resources.ResourceReader";
+        internal const string ResSetTypeName = "System.Resources.RuntimeResourceSet";
         internal const string ResFileExtension = ".resources";
         internal const int ResFileExtensionLength = 10;
 
@@ -274,7 +271,7 @@ namespace System.Resources
             MainAssembly = assembly;
             BaseNameField = baseName;
 
-            if (usingResourceSet != null && (usingResourceSet != _minResourceSet) && !(usingResourceSet.IsSubclassOf(_minResourceSet)))
+            if (usingResourceSet != null && (usingResourceSet != s_minResourceSet) && !(usingResourceSet.IsSubclassOf(s_minResourceSet)))
                 throw new ArgumentException(SR.Arg_ResMgrNotResSet, nameof(usingResourceSet));
             _userResourceSet = usingResourceSet;
 
@@ -648,61 +645,31 @@ namespace System.Resources
         }
 
         // IGNORES VERSION
-        internal static bool CompareNames(string asmTypeName1,
-                                          string typeName2,
-                                          AssemblyName asmName2)
+        internal static bool IsDefaultType(string asmTypeName,
+                                           string typeName)
         {
-            Debug.Assert(asmTypeName1 != null, "asmTypeName1 was unexpectedly null");
+            Debug.Assert(asmTypeName != null, "asmTypeName was unexpectedly null");
 
             // First, compare type names
-            int comma = asmTypeName1.IndexOf(',');
-            if (((comma == -1) ? asmTypeName1.Length : comma) != typeName2.Length)
+            int comma = asmTypeName.IndexOf(',');
+            if (((comma == -1) ? asmTypeName.Length : comma) != typeName.Length)
                 return false;
 
             // case sensitive
-            if (string.Compare(asmTypeName1, 0, typeName2, 0, typeName2.Length, StringComparison.Ordinal) != 0)
+            if (string.Compare(asmTypeName, 0, typeName, 0, typeName.Length, StringComparison.Ordinal) != 0)
                 return false;
             if (comma == -1)
                 return true;
 
             // Now, compare assembly display names (IGNORES VERSION AND PROCESSORARCHITECTURE)
             // also, for  mscorlib ignores everything, since that's what the binder is going to do
-            while (char.IsWhiteSpace(asmTypeName1[++comma])) ;
+            while (char.IsWhiteSpace(asmTypeName[++comma])) ;
 
             // case insensitive
-            AssemblyName an1 = new AssemblyName(asmTypeName1.Substring(comma));
-            if (!string.Equals(an1.Name, asmName2.Name, StringComparison.OrdinalIgnoreCase))
-                return false;
+            AssemblyName an = new AssemblyName(asmTypeName.Substring(comma));
 
             // to match IsMscorlib() in VM
-            if (string.Equals(an1.Name, System.CoreLib.Name, StringComparison.OrdinalIgnoreCase))
-                return true;
-
-
-            if ((an1.CultureInfo != null) && (asmName2.CultureInfo != null) &&
-#if FEATURE_USE_LCID                
-                (an1.CultureInfo.LCID != asmName2.CultureInfo.LCID)
-#else
-                (an1.CultureInfo.Name != asmName2.CultureInfo.Name)
-#endif                
-                )
-                return false;
-
-            byte[] pkt1 = an1.GetPublicKeyToken();
-            byte[] pkt2 = asmName2.GetPublicKeyToken();
-            if ((pkt1 != null) && (pkt2 != null))
-            {
-                if (pkt1.Length != pkt2.Length)
-                    return false;
-
-                for (int i = 0; i < pkt1.Length; i++)
-                {
-                    if (pkt1[i] != pkt2[i])
-                        return false;
-                }
-            }
-
-            return true;
+            return string.Equals(an.Name, "mscorlib", StringComparison.OrdinalIgnoreCase);
         }
 
 #if FEATURE_APPX