Remove usage of non-generic collections from Mono's SPC (#35312)
authorRoman Marusyk <Marusyk@users.noreply.github.com>
Mon, 27 Apr 2020 12:13:27 +0000 (15:13 +0300)
committerGitHub <noreply@github.com>
Mon, 27 Apr 2020 12:13:27 +0000 (12:13 +0000)
* Remove usage of non-generic collections from Mono's SPC #35282

* Remove usage of non-generic collections from Mono's SPC

Fix #35282

Co-authored-by: Alexander Köplinger <alex.koeplinger@outlook.com>
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/EventOnTypeBuilderInst.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.Mono.cs
src/mono/netcore/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs

index f2f088b..d7a71d3 100644 (file)
@@ -28,7 +28,7 @@
 //
 
 #if MONO_FEATURE_SRE
-using System.Collections;
+using System.Collections.Generic;
 using System.Runtime.InteropServices;
 
 namespace System.Reflection.Emit
@@ -90,15 +90,13 @@ namespace System.Reflection.Emit
             if (other == null)
                 return Array.Empty<MethodInfo>();
 
-            ArrayList ar = new ArrayList();
+            List<MethodInfo> res = new List<MethodInfo>();
             foreach (MethodInfo method in other)
             {
                 if (nonPublic || method.IsPublic)
-                    ar.Add(TypeBuilder.GetMethod(instantiation, method));
+                    res.Add(TypeBuilder.GetMethod(instantiation, method));
             }
-            MethodInfo[] res = new MethodInfo[ar.Count];
-            ar.CopyTo(res, 0);
-            return res;
+            return res.ToArray();
         }
 
         public override Type DeclaringType
index ef85599..00bcc90 100644 (file)
@@ -32,7 +32,7 @@
 //
 
 #if MONO_FEATURE_SRE
-using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
 using System.Runtime.CompilerServices;
@@ -974,7 +974,7 @@ namespace System.Reflection.Emit
         {
             if (ctors == null)
                 return Array.Empty<ConstructorInfo>();
-            ArrayList l = new ArrayList();
+            List<ConstructorInfo> result = new List<ConstructorInfo>();
             bool match;
             MethodAttributes mattrs;
 
@@ -1007,11 +1007,9 @@ namespace System.Reflection.Emit
                 }
                 if (!match)
                     continue;
-                l.Add(c);
+                result.Add(c);
             }
-            ConstructorInfo[] result = new ConstructorInfo[l.Count];
-            l.CopyTo(result);
-            return result;
+            return result.ToArray();
         }
 
         public override Type GetElementType()
@@ -1096,7 +1094,7 @@ namespace System.Reflection.Emit
             if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null))
             {
                 MethodInfo[] parent_methods = parent.GetMethods(bindingAttr);
-                ArrayList parent_candidates = new ArrayList(parent_methods.Length);
+                List<MethodInfo> parent_candidates = new List<MethodInfo>(parent_methods.Length);
 
                 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
 
@@ -1139,7 +1137,7 @@ namespace System.Reflection.Emit
             if (candidates == null)
                 return Array.Empty<MethodInfo>();
 
-            ArrayList l = new ArrayList();
+            List<MethodInfo> result = new List<MethodInfo>();
 
             foreach (MethodInfo c in candidates)
             {
@@ -1177,12 +1175,9 @@ namespace System.Reflection.Emit
                 }
                 if (!match)
                     continue;
-                l.Add(c);
+                result.Add(c);
             }
-
-            MethodInfo[] result = new MethodInfo[l.Count];
-            l.CopyTo(result);
-            return result;
+            return result.ToArray();
         }
 
         public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
@@ -1239,7 +1234,7 @@ namespace System.Reflection.Emit
                 throw new NotSupportedException();
 
             bool match;
-            ArrayList result = new ArrayList();
+            List<Type> result = new List<Type>();
 
             if (subtypes == null)
                 return EmptyTypes;
@@ -1260,9 +1255,7 @@ namespace System.Reflection.Emit
                     continue;
                 result.Add(t);
             }
-            Type[] r = new Type[result.Count];
-            result.CopyTo(r);
-            return r;
+            return result.ToArray();
         }
 
         public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
index 44978aa..60335b4 100644 (file)
@@ -32,7 +32,7 @@
 //
 
 #if MONO_FEATURE_SRE
-using System.Collections;
+using System.Collections.Generic;
 using System.Globalization;
 using System.Text;
 using System.Runtime.InteropServices;
@@ -53,7 +53,9 @@ namespace System.Reflection.Emit
 #pragma warning restore 649
         #endregion
 
-        private Hashtable? fields, ctors, methods;
+        private Dictionary<FieldInfo, FieldInfo>? fields;
+        private Dictionary<ConstructorInfo, ConstructorInfo>? ctors;
+        private Dictionary<MethodInfo, MethodInfo>? methods;
 
         internal TypeBuilderInstantiation()
         {
@@ -167,28 +169,28 @@ namespace System.Reflection.Emit
         internal override MethodInfo GetMethod(MethodInfo fromNoninstanciated)
         {
             if (methods == null)
-                methods = new Hashtable();
+                methods = new Dictionary<MethodInfo, MethodInfo>();
             if (!methods.ContainsKey(fromNoninstanciated))
                 methods[fromNoninstanciated] = new MethodOnTypeBuilderInst(this, fromNoninstanciated);
-            return (MethodInfo)methods[fromNoninstanciated]!;
+            return methods[fromNoninstanciated]!;
         }
 
         internal override ConstructorInfo GetConstructor(ConstructorInfo fromNoninstanciated)
         {
             if (ctors == null)
-                ctors = new Hashtable();
+                ctors = new Dictionary<ConstructorInfo, ConstructorInfo>();
             if (!ctors.ContainsKey(fromNoninstanciated))
                 ctors[fromNoninstanciated] = new ConstructorOnTypeBuilderInst(this, fromNoninstanciated);
-            return (ConstructorInfo)ctors[fromNoninstanciated]!;
+            return ctors[fromNoninstanciated]!;
         }
 
         internal override FieldInfo GetField(FieldInfo fromNoninstanciated)
         {
             if (fields == null)
-                fields = new Hashtable();
+                fields = new Dictionary<FieldInfo, FieldInfo>();
             if (!fields.ContainsKey(fromNoninstanciated))
                 fields[fromNoninstanciated] = new FieldOnTypeBuilderInst(this, fromNoninstanciated);
-            return (FieldInfo)fields[fromNoninstanciated]!;
+            return fields[fromNoninstanciated]!;
         }
 
         public override MethodInfo[] GetMethods(BindingFlags bf)