Remove use of RuntimeReflectionExtensions from Microsoft.CSharp (dotnet/corefx#41143)
authorStephen Toub <stoub@microsoft.com>
Tue, 17 Sep 2019 00:38:32 +0000 (20:38 -0400)
committerGitHub <noreply@github.com>
Tue, 17 Sep 2019 00:38:32 +0000 (20:38 -0400)
* Remove use of RuntimeReflectionExtensions from Microsoft.CSharp

The wrappers aren't adding any benefit and are causing us to pull in the extensions type unnecessarily (and doing unnecessary null checks); just call the underlying methods directly.

* Address PR feedback

Commit migrated from https://github.com/dotnet/corefx/commit/4dd07b79be316cf8421a51e0ed6b4a145713080e

src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Tree/MethodInfo.cs
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/SymbolTable.cs

index 0e91df0..4db466a 100644 (file)
@@ -44,7 +44,8 @@ namespace Microsoft.CSharp.RuntimeBinder.Semantics
                 }
 
                 // We need to find the associated methodinfo on the instantiated type.
-                foreach (MethodInfo m in type.GetRuntimeMethods())
+                const BindingFlags EverythingBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
+                foreach (MethodInfo m in type.GetMethods(EverythingBindingFlags))
                 {
                     if (!m.HasSameMetadataDefinitionAs(methodInfo))
                     {
index 1b609bc..1a81940 100644 (file)
@@ -17,6 +17,7 @@ namespace Microsoft.CSharp.RuntimeBinder
 {
     internal static class SymbolTable
     {
+        private const BindingFlags EverythingBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
         private static readonly HashSet<Type> s_typesWithConversionsLoaded = new HashSet<Type>();
         private static readonly HashSet<NameHashKey> s_namesLoadedForEachType = new HashSet<NameHashKey>();
 
@@ -161,8 +162,7 @@ namespace Microsoft.CSharp.RuntimeBinder
 
                 // Now loop over all methods and add them.
                 IEnumerator<MemberInfo> memberEn = type
-                    .GetMembers(
-                        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
+                    .GetMembers(EverythingBindingFlags)
                     .Where(member => member.DeclaringType == type && member.Name == name).GetEnumerator();
                 if (memberEn.MoveNext())
                 {
@@ -747,9 +747,9 @@ namespace Microsoft.CSharp.RuntimeBinder
                 {
                     MethodBase methodBase = t.DeclaringMethod;
                     bool bAdded = false;
-                    foreach (MethodInfo methinfo in Enumerable.Where(t.DeclaringType.GetRuntimeMethods(), m => m.HasSameMetadataDefinitionAs(methodBase)))
+                    foreach (MethodInfo methinfo in t.DeclaringType.GetMethods(EverythingBindingFlags))
                     {
-                        if (!methinfo.IsGenericMethod)
+                        if (!methinfo.HasSameMetadataDefinitionAs(methodBase) || !methinfo.IsGenericMethod)
                         {
                             continue;
                         }
@@ -1192,11 +1192,12 @@ namespace Microsoft.CSharp.RuntimeBinder
             AggregateType aggtype = type.getThisType();
             Type t = aggtype.AssociatedSystemType;
 
-            var props = Enumerable.Where(t.GetRuntimeProperties(), x => x.Name == property.Text);
-
-            foreach (PropertyInfo pi in props)
+            foreach (PropertyInfo pi in t.GetProperties(EverythingBindingFlags))
             {
-                AddPropertyToSymbolTable(pi, type);
+                if (pi.Name == property.Text)
+                {
+                    AddPropertyToSymbolTable(pi, type);
+                }
             }
         }
 
@@ -1289,7 +1290,7 @@ namespace Microsoft.CSharp.RuntimeBinder
             if (property.GetMethod != null || property.SetMethod != null)
             {
                 MethodInfo accessor = property.GetMethod ?? property.SetMethod; // Must have at least one.
-                prop.isOverride = accessor.IsVirtual && accessor.IsHideBySig && accessor.GetRuntimeBaseDefinition() != accessor;
+                prop.isOverride = accessor.IsVirtual && accessor.IsHideBySig && accessor.GetBaseDefinition() != accessor;
                 prop.isHideByName = !accessor.IsHideBySig;
             }
 
@@ -1370,14 +1371,15 @@ namespace Microsoft.CSharp.RuntimeBinder
             }
             else
             {
-                var methods = Enumerable.Where(t.GetRuntimeMethods(), m => m.Name == methodName.Text && m.DeclaringType == t);
-
-                foreach (MethodInfo m in methods)
+                foreach (MethodInfo m in t.GetMethods(EverythingBindingFlags))
                 {
-                    AddMethodToSymbolTable(
-                        m,
-                        type,
-                        m.Name == SpecialNames.Invoke ? MethodKindEnum.Invoke : MethodKindEnum.Actual);
+                    if (m.Name == methodName.Text && m.DeclaringType == t)
+                    {
+                        AddMethodToSymbolTable(
+                            m,
+                            type,
+                            m.Name == SpecialNames.Invoke ? MethodKindEnum.Invoke : MethodKindEnum.Actual);
+                    }
                 }
             }
         }
@@ -1457,7 +1459,7 @@ namespace Microsoft.CSharp.RuntimeBinder
             if (method != null)
             {
                 methodSymbol.typeVars = GetMethodTypeParameters(method, methodSymbol);
-                methodSymbol.isOverride = method.IsVirtual && method.IsHideBySig && method.GetRuntimeBaseDefinition() != method;
+                methodSymbol.isOverride = method.IsVirtual && method.IsHideBySig && method.GetBaseDefinition() != method;
                 methodSymbol.isOperator = IsOperator(method);
                 methodSymbol.swtSlot = GetSlotForOverride(method);
                 methodSymbol.RetType = GetCTypeFromType(method.ReturnType);
@@ -1727,7 +1729,7 @@ namespace Microsoft.CSharp.RuntimeBinder
         {
             if (method.IsVirtual && method.IsHideBySig)
             {
-                MethodInfo baseMethodInfo = method.GetRuntimeBaseDefinition();
+                MethodInfo baseMethodInfo = method.GetBaseDefinition();
                 if (baseMethodInfo == method)
                 {
                     // We just found ourselves, so we don't care here.
@@ -1832,10 +1834,9 @@ namespace Microsoft.CSharp.RuntimeBinder
             AggregateSymbol aggregate = ((AggregateType)t).OwningAggregate;
 
             // Now find all the conversions and make them.
-            foreach (MethodInfo conversion in type.GetRuntimeMethods())
+            foreach (MethodInfo conversion in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
             {
-                if (conversion.IsPublic && conversion.IsStatic && conversion.DeclaringType == type
-                    && conversion.IsSpecialName && !conversion.IsGenericMethod)
+                if (conversion.DeclaringType == type && conversion.IsSpecialName && !conversion.IsGenericMethod)
                 {
                     MethodKindEnum methodKind;
                     switch (conversion.Name)