From 0a7431b7c78fd95da5c28a746b034ec0a6729494 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 16 Sep 2019 20:38:32 -0400 Subject: [PATCH] Remove use of RuntimeReflectionExtensions from Microsoft.CSharp (dotnet/corefx#41143) * 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 --- .../RuntimeBinder/Semantics/Tree/MethodInfo.cs | 3 +- .../Microsoft/CSharp/RuntimeBinder/SymbolTable.cs | 43 +++++++++++----------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Tree/MethodInfo.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Tree/MethodInfo.cs index 0e91df0..4db466a 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Tree/MethodInfo.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Tree/MethodInfo.cs @@ -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)) { diff --git a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/SymbolTable.cs b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/SymbolTable.cs index 1b609bc..1a81940 100644 --- a/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/SymbolTable.cs +++ b/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/SymbolTable.cs @@ -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 s_typesWithConversionsLoaded = new HashSet(); private static readonly HashSet s_namesLoadedForEachType = new HashSet(); @@ -161,8 +162,7 @@ namespace Microsoft.CSharp.RuntimeBinder // Now loop over all methods and add them. IEnumerator 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) -- 2.7.4