Drop Convert static constructor dependency from RuntimeType (#45054)
authorMarek Safar <marek.safar@gmail.com>
Sun, 22 Nov 2020 06:06:04 +0000 (07:06 +0100)
committerGitHub <noreply@github.com>
Sun, 22 Nov 2020 06:06:04 +0000 (22:06 -0800)
* Drop Convert static constructor dependency from RuntimeTypeto make it trimmable

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs
src/libraries/System.Private.CoreLib/src/System/Type.cs

index e30ad7b..6bc3ddf 100644 (file)
@@ -194,23 +194,36 @@ namespace System
                     typeCode = TypeCode.Single; break;
                 case CorElementType.ELEMENT_TYPE_R8:
                     typeCode = TypeCode.Double; break;
+#if !CORECLR
+                case CorElementType.ELEMENT_TYPE_STRING:
+                    typeCode = TypeCode.String; break;
+#endif
                 case CorElementType.ELEMENT_TYPE_VALUETYPE:
-                    if (this == Convert.ConvertTypes[(int)TypeCode.Decimal])
+                    if (ReferenceEquals(this, typeof(decimal)))
                         typeCode = TypeCode.Decimal;
-                    else if (this == Convert.ConvertTypes[(int)TypeCode.DateTime])
+                    else if (ReferenceEquals(this, typeof(DateTime)))
                         typeCode = TypeCode.DateTime;
                     else if (IsEnum)
-                        typeCode = GetTypeCode(Enum.GetUnderlyingType(this));
+                        typeCode = GetTypeCode(Enum.InternalGetUnderlyingType(this));
                     else
                         typeCode = TypeCode.Object;
                     break;
                 default:
-                    if (this == Convert.ConvertTypes[(int)TypeCode.DBNull])
-                        typeCode = TypeCode.DBNull;
-                    else if (this == Convert.ConvertTypes[(int)TypeCode.String])
+#if CORECLR
+                    // GetSignatureCorElementType returns E_T_CLASS for E_T_STRING
+                    if (ReferenceEquals(this, typeof(string)))
+                    {
                         typeCode = TypeCode.String;
-                    else
-                        typeCode = TypeCode.Object;
+                        break;
+                    }
+#endif
+                    if (ReferenceEquals(this, typeof(DBNull)))
+                    {
+                        typeCode = TypeCode.DBNull;
+                        break;
+                    }
+
+                    typeCode = TypeCode.Object;
                     break;
             }
 
index b25a7a0..fe4f171 100644 (file)
@@ -424,15 +424,14 @@ namespace System
 
         public static TypeCode GetTypeCode(Type? type)
         {
-            if (type == null)
-                return TypeCode.Empty;
-            return type.GetTypeCodeImpl();
+            return type?.GetTypeCodeImpl() ?? TypeCode.Empty;
         }
+
         protected virtual TypeCode GetTypeCodeImpl()
         {
             Type systemType = UnderlyingSystemType;
-            if (this != systemType && systemType != null)
-                return Type.GetTypeCode(systemType);
+            if (!ReferenceEquals(this, systemType) && systemType is not null)
+                return GetTypeCode(systemType);
 
             return TypeCode.Object;
         }