From 11631e7412197f3f803ebbef95a3ddb174a387ec Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 13 Apr 2018 15:10:10 -0700 Subject: [PATCH] Clean up 'GetTypeInfo()' calls in engine folder (#6634) 'GetTypeInfo()' were added when porting PowerShell to early version of .NET Core (prior .NET Core 1.0). Most properties and methods in 'System.Type' were missing at that time. Now the call is not needed anymore and should be removed. --- .../engine/ArgumentTypeConverterAttribute.cs | 2 +- .../engine/CmdletInfo.cs | 2 +- .../engine/CommandMetadata.cs | 2 +- .../engine/CompiledCommandParameter.cs | 9 ++++----- .../engine/EnumExpressionEvaluator.cs | 4 ++-- .../engine/EventManager.cs | 2 +- .../engine/GetCommandCommand.cs | 6 +++--- .../engine/InitialSessionState.cs | 16 +++++++--------- .../engine/MshCmdlet.cs | 2 +- .../engine/MshObject.cs | 15 +++++++-------- .../engine/ParameterBinderBase.cs | 9 ++++----- .../engine/ReflectionParameterBinder.cs | 2 +- .../engine/SessionStateContainer.cs | 2 +- .../engine/ThirdPartyAdapter.cs | 2 +- .../engine/cmdlet.cs | 2 +- .../engine/serialization.cs | 6 +++--- 16 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/System.Management.Automation/engine/ArgumentTypeConverterAttribute.cs b/src/System.Management.Automation/engine/ArgumentTypeConverterAttribute.cs index a761e2fd0..667147721 100644 --- a/src/System.Management.Automation/engine/ArgumentTypeConverterAttribute.cs +++ b/src/System.Management.Automation/engine/ArgumentTypeConverterAttribute.cs @@ -185,7 +185,7 @@ namespace System.Management.Automation } else { - bool isNullable = boolType.GetTypeInfo().IsGenericType && + bool isNullable = boolType.IsGenericType && boolType.GetGenericTypeDefinition() == typeof(Nullable<>); if (!isNullable && LanguagePrimitives.IsBooleanType(boolType)) diff --git a/src/System.Management.Automation/engine/CmdletInfo.cs b/src/System.Management.Automation/engine/CmdletInfo.cs index 832a4614a..b12c2a871 100644 --- a/src/System.Management.Automation/engine/CmdletInfo.cs +++ b/src/System.Management.Automation/engine/CmdletInfo.cs @@ -340,7 +340,7 @@ namespace System.Management.Automation if (ImplementingType != null) { - foreach (object o in ImplementingType.GetTypeInfo().GetCustomAttributes(typeof(OutputTypeAttribute), false)) + foreach (object o in ImplementingType.GetCustomAttributes(typeof(OutputTypeAttribute), false)) { OutputTypeAttribute attr = (OutputTypeAttribute)o; _outputType.AddRange(attr.Type); diff --git a/src/System.Management.Automation/engine/CommandMetadata.cs b/src/System.Management.Automation/engine/CommandMetadata.cs index e9db54d31..29a0602ab 100644 --- a/src/System.Management.Automation/engine/CommandMetadata.cs +++ b/src/System.Management.Automation/engine/CommandMetadata.cs @@ -697,7 +697,7 @@ namespace System.Management.Automation // Process the attributes on the cmdlet - var customAttributes = CommandType.GetTypeInfo().GetCustomAttributes(false); + var customAttributes = CommandType.GetCustomAttributes(false); foreach (Attribute attribute in customAttributes) { diff --git a/src/System.Management.Automation/engine/CompiledCommandParameter.cs b/src/System.Management.Automation/engine/CompiledCommandParameter.cs index 3f879d6d1..bfaafef87 100644 --- a/src/System.Management.Automation/engine/CompiledCommandParameter.cs +++ b/src/System.Management.Automation/engine/CompiledCommandParameter.cs @@ -613,7 +613,6 @@ namespace System.Management.Automation { ParameterCollectionType = ParameterCollectionType.NotCollection; Diagnostics.Assert(type != null, "Caller to verify type argument"); - TypeInfo typeInfo = type.GetTypeInfo(); // NTRAID#Windows OS Bugs-1009284-2004/05/11-JeffJon // What other collection types should be supported? @@ -636,8 +635,8 @@ namespace System.Management.Automation } Type[] interfaces = type.GetInterfaces(); - if (interfaces.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)) - || (typeInfo.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>))) + if (interfaces.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)) + || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>))) { return; } @@ -648,7 +647,7 @@ namespace System.Management.Automation // is more efficient to bind than ICollection. This optimization // retrieves the element type so that we can coerce the elements. // Otherwise they must already be the right type. - if (implementsIList && typeInfo.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Collection<>))) + if (implementsIList && type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Collection<>))) { ParameterCollectionType = ParameterCollectionType.IList; // figure out elementType @@ -666,7 +665,7 @@ namespace System.Management.Automation // to an ICollection is via reflected calls to Add(T), // but the advantage over plain IList is that we can typecast the elements. Type interfaceICollection = - interfaces.FirstOrDefault(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)); + interfaces.FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)); if (interfaceICollection != null) { // We only deal with the first type for which ICollection is implemented diff --git a/src/System.Management.Automation/engine/EnumExpressionEvaluator.cs b/src/System.Management.Automation/engine/EnumExpressionEvaluator.cs index d88f59e27..68de01a99 100644 --- a/src/System.Management.Automation/engine/EnumExpressionEvaluator.cs +++ b/src/System.Management.Automation/engine/EnumExpressionEvaluator.cs @@ -26,7 +26,7 @@ namespace System.Management.Automation /// public FlagsExpression(string expression) { - if (!typeof(T).GetTypeInfo().IsEnum) + if (!typeof(T).IsEnum) { throw InterpreterError.NewInterpreterException(expression, typeof(RuntimeException), null, "InvalidGenericType", EnumExpressionEvaluatorStrings.InvalidGenericType); @@ -58,7 +58,7 @@ namespace System.Management.Automation /// public FlagsExpression(object[] expression) { - if (!typeof(T).GetTypeInfo().IsEnum) + if (!typeof(T).IsEnum) { throw InterpreterError.NewInterpreterException(expression, typeof(RuntimeException), null, "InvalidGenericType", EnumExpressionEvaluatorStrings.InvalidGenericType); diff --git a/src/System.Management.Automation/engine/EventManager.cs b/src/System.Management.Automation/engine/EventManager.cs index 4e8f129e9..86e2e7857 100644 --- a/src/System.Management.Automation/engine/EventManager.cs +++ b/src/System.Management.Automation/engine/EventManager.cs @@ -1464,7 +1464,7 @@ namespace System.Management.Automation methodContents.Emit(OpCodes.Ldarg, counter); // Box the value type if necessary - if (parameterTypes[counter - 1].GetTypeInfo().IsValueType) + if (parameterTypes[counter - 1].IsValueType) { methodContents.Emit(OpCodes.Box, parameterTypes[counter - 1]); } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index a05047c91..4c0f522cc 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1472,8 +1472,8 @@ namespace Microsoft.PowerShell.Commands private static PSObject GetParameterType(Type parameterType) { PSObject returnParameterType = new PSObject(); - bool isEnum = parameterType.GetTypeInfo().IsEnum; - bool isArray = parameterType.GetTypeInfo().IsArray; + bool isEnum = parameterType.IsEnum; + bool isArray = parameterType.IsArray; returnParameterType.Properties.Add(new PSNoteProperty("FullName", parameterType.FullName)); returnParameterType.Properties.Add(new PSNoteProperty("IsEnum", isEnum)); returnParameterType.Properties.Add(new PSNoteProperty("IsArray", isArray)); @@ -1483,7 +1483,7 @@ namespace Microsoft.PowerShell.Commands returnParameterType.Properties.Add(new PSNoteProperty("EnumValues", enumValues)); bool hasFlagAttribute = (isArray) ? - ((parameterType.GetTypeInfo().GetCustomAttributes(typeof(FlagsAttribute), true)).Count() > 0) : false; + ((parameterType.GetCustomAttributes(typeof(FlagsAttribute), true)).Count() > 0) : false; returnParameterType.Properties.Add(new PSNoteProperty("HasFlagAttribute", hasFlagAttribute)); // Recurse into array elements. diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 582199fb0..4bde9021c 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4976,9 +4976,9 @@ if($paths) { return assembly; } - private static T GetCustomAttribute(TypeInfo decoratedType) where T : Attribute + private static T GetCustomAttribute(Type decoratedType) where T : Attribute { - var attributes = CustomAttributeExtensions.GetCustomAttributes(decoratedType, false); + var attributes = decoratedType.GetCustomAttributes(false); var customAttrs = attributes.ToArray(); Debug.Assert(customAttrs.Length <= 1, "CmdletAttribute and/or CmdletProviderAttribute cannot normally appear more than once"); @@ -5238,8 +5238,7 @@ if($paths) { foreach (Type type in assemblyTypes) { - var typeInfo = type.GetTypeInfo(); - if (!(typeInfo.IsPublic || typeInfo.IsNestedPublic) || typeInfo.IsAbstract) + if (!(type.IsPublic || type.IsNestedPublic) || type.IsAbstract) continue; // Check for cmdlets @@ -5247,7 +5246,7 @@ if($paths) { { randomCmdletToCheckLinkDemand = type; - CmdletAttribute cmdletAttribute = GetCustomAttribute(typeInfo); + CmdletAttribute cmdletAttribute = GetCustomAttribute(type); if (cmdletAttribute == null) { continue; @@ -5275,7 +5274,7 @@ if($paths) { } cmdlets.Add(cmdletName, cmdlet); - var aliasAttribute = GetCustomAttribute(typeInfo); + var aliasAttribute = GetCustomAttribute(type); if (aliasAttribute != null) { if (aliases == null) @@ -5309,7 +5308,7 @@ if($paths) { { randomProviderToCheckLinkDemand = type; - CmdletProviderAttribute providerAttribute = GetCustomAttribute(typeInfo); + CmdletProviderAttribute providerAttribute = GetCustomAttribute(type); if (providerAttribute == null) { continue; @@ -5453,8 +5452,7 @@ if($paths) { for (int i = 0; i < assemblyTypes.Length; i++) { Type type = assemblyTypes[i]; - TypeInfo typeInfo = type.GetTypeInfo(); - if (!(typeInfo.IsPublic || typeInfo.IsNestedPublic) || typeInfo.IsAbstract) { continue; } + if (!(type.IsPublic || type.IsNestedPublic) || type.IsAbstract) { continue; } if (isModuleLoad && typeof(IModuleAssemblyInitializer).IsAssignableFrom(type) && type != typeof(IModuleAssemblyInitializer)) { diff --git a/src/System.Management.Automation/engine/MshCmdlet.cs b/src/System.Management.Automation/engine/MshCmdlet.cs index 171198b82..74bac7b7e 100644 --- a/src/System.Management.Automation/engine/MshCmdlet.cs +++ b/src/System.Management.Automation/engine/MshCmdlet.cs @@ -444,7 +444,7 @@ namespace System.Management.Automation } CmdletAttribute ca = null; - foreach (var attr in cmdletType.GetTypeInfo().GetCustomAttributes(true)) + foreach (var attr in cmdletType.GetCustomAttributes(true)) { ca = attr as CmdletAttribute; if (ca != null) diff --git a/src/System.Management.Automation/engine/MshObject.cs b/src/System.Management.Automation/engine/MshObject.cs index 2bfb73f87..f786edd6e 100644 --- a/src/System.Management.Automation/engine/MshObject.cs +++ b/src/System.Management.Automation/engine/MshObject.cs @@ -2407,8 +2407,7 @@ namespace Microsoft.PowerShell return String.Empty; string result; - TypeInfo typeinfo = type.GetTypeInfo(); - if (typeinfo.IsGenericType && !typeinfo.IsGenericTypeDefinition) + if (type.IsGenericType && !type.IsGenericTypeDefinition) { string genericDefinition = Type(type.GetGenericTypeDefinition(), dropNamespaces); // For regular generic types, we find the backtick character, for example: @@ -2417,12 +2416,12 @@ namespace Microsoft.PowerShell // For nested generic types, we find the left bracket character, for example: // System.Collections.Generic.Dictionary`2+Enumerator[TKey, TValue] -> // System.Collections.Generic.Dictionary`2+Enumerator[string,string] - int backtickOrLeftBracketIndex = genericDefinition.LastIndexOf(typeinfo.IsNested ? '[' : '`'); + int backtickOrLeftBracketIndex = genericDefinition.LastIndexOf(type.IsNested ? '[' : '`'); var sb = new StringBuilder(genericDefinition, 0, backtickOrLeftBracketIndex, 512); AddGenericArguments(sb, type.GetGenericArguments(), dropNamespaces); result = sb.ToString(); } - else if (typeinfo.IsArray) + else if (type.IsArray) { string elementDefinition = Type(type.GetElementType(), dropNamespaces); var sb = new StringBuilder(elementDefinition, elementDefinition.Length + 10); @@ -2445,7 +2444,7 @@ namespace Microsoft.PowerShell } if (dropNamespaces) { - if (typeinfo.IsNested) + if (type.IsNested) { // For nested types, we should return OuterType+InnerType. For example, // System.Environment+SpecialFolder -> Environment+SpecialFolder @@ -2468,10 +2467,10 @@ namespace Microsoft.PowerShell // We can't round trip anything with a generic parameter. // We also can't round trip if we're dropping the namespace. - if (!typeinfo.IsGenericParameter - && !typeinfo.ContainsGenericParameters + if (!type.IsGenericParameter + && !type.ContainsGenericParameters && !dropNamespaces - && !typeinfo.Assembly.GetCustomAttributes(typeof(DynamicClassImplementationAssemblyAttribute)).Any()) + && !type.Assembly.GetCustomAttributes(typeof(DynamicClassImplementationAssemblyAttribute)).Any()) { Type roundTripType; TypeResolver.TryResolveType(result, out roundTripType); diff --git a/src/System.Management.Automation/engine/ParameterBinderBase.cs b/src/System.Management.Automation/engine/ParameterBinderBase.cs index 983683e0b..b7e81e363 100644 --- a/src/System.Management.Automation/engine/ParameterBinderBase.cs +++ b/src/System.Management.Automation/engine/ParameterBinderBase.cs @@ -850,7 +850,7 @@ namespace System.Management.Automation { return parameterType == null || isDefaultValue || - (!parameterType.GetTypeInfo().IsValueType && + (!parameterType.IsValueType && parameterType != typeof(string)); } @@ -1245,14 +1245,13 @@ namespace System.Management.Automation // we don't want to attempt to bind a collection to a scalar unless // the parameter type is Object or PSObject or enum. - TypeInfo toTypeInfo = toType.GetTypeInfo(); if (GetIList(currentValue) != null && toType != typeof(Object) && toType != typeof(PSObject) && toType != typeof(PSListModifier) && - (!toTypeInfo.IsGenericType || toTypeInfo.GetGenericTypeDefinition() != typeof(PSListModifier<>)) && - (!toTypeInfo.IsGenericType || toTypeInfo.GetGenericTypeDefinition() != typeof(FlagsExpression<>)) && - !toTypeInfo.IsEnum) + (!toType.IsGenericType || toType.GetGenericTypeDefinition() != typeof(PSListModifier<>)) && + (!toType.IsGenericType || toType.GetGenericTypeDefinition() != typeof(FlagsExpression<>)) && + !toType.IsEnum) { throw new NotSupportedException(); } diff --git a/src/System.Management.Automation/engine/ReflectionParameterBinder.cs b/src/System.Management.Automation/engine/ReflectionParameterBinder.cs index 6d47c93b8..eda690078 100644 --- a/src/System.Management.Automation/engine/ReflectionParameterBinder.cs +++ b/src/System.Management.Automation/engine/ReflectionParameterBinder.cs @@ -242,7 +242,7 @@ namespace System.Management.Automation var propertyExpr = GetPropertyOrFieldExpr(type, property, Expression.Convert(target, type)); Expression expr = Expression.Assign(propertyExpr, Expression.Convert(value, propertyExpr.Type)); - if (propertyExpr.Type.GetTypeInfo().IsValueType && Nullable.GetUnderlyingType(propertyExpr.Type) == null) + if (propertyExpr.Type.IsValueType && Nullable.GetUnderlyingType(propertyExpr.Type) == null) { var throwInvalidCastExceptionExpr = Expression.Call(Language.CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException, diff --git a/src/System.Management.Automation/engine/SessionStateContainer.cs b/src/System.Management.Automation/engine/SessionStateContainer.cs index 3485c3f49..64fda8da8 100644 --- a/src/System.Management.Automation/engine/SessionStateContainer.cs +++ b/src/System.Management.Automation/engine/SessionStateContainer.cs @@ -2285,7 +2285,7 @@ namespace System.Management.Automation { mi = providerType.GetMethod("GetChildItemsDynamicParameters", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); - providerType = providerType.GetTypeInfo().BaseType; + providerType = providerType.BaseType; } while ( (mi == null) && (providerType != null) && diff --git a/src/System.Management.Automation/engine/ThirdPartyAdapter.cs b/src/System.Management.Automation/engine/ThirdPartyAdapter.cs index c3b64c3c6..7f5b199e2 100644 --- a/src/System.Management.Automation/engine/ThirdPartyAdapter.cs +++ b/src/System.Management.Automation/engine/ThirdPartyAdapter.cs @@ -279,7 +279,7 @@ namespace System.Management.Automation Collection types = new Collection(); - for (Type type = baseObject.GetType(); type != null; type = type.GetTypeInfo().BaseType) + for (Type type = baseObject.GetType(); type != null; type = type.BaseType) { types.Add(type.FullName); } diff --git a/src/System.Management.Automation/engine/cmdlet.cs b/src/System.Management.Automation/engine/cmdlet.cs index df54de53d..8d1574768 100644 --- a/src/System.Management.Automation/engine/cmdlet.cs +++ b/src/System.Management.Automation/engine/cmdlet.cs @@ -256,7 +256,7 @@ namespace System.Management.Automation if (String.IsNullOrEmpty(resourceId)) throw PSTraceSource.NewArgumentNullException("resourceId"); - ResourceManager manager = ResourceManagerCache.GetResourceManager(this.GetType().GetTypeInfo().Assembly, baseName); + ResourceManager manager = ResourceManagerCache.GetResourceManager(this.GetType().Assembly, baseName); string retValue = null; try diff --git a/src/System.Management.Automation/engine/serialization.cs b/src/System.Management.Automation/engine/serialization.cs index 4da750168..0d3e8adf0 100644 --- a/src/System.Management.Automation/engine/serialization.cs +++ b/src/System.Management.Automation/engine/serialization.cs @@ -5308,7 +5308,7 @@ namespace System.Management.Automation else { Type gt = source.GetType(); - if (gt.GetTypeInfo().IsGenericType) + if (gt.IsGenericType) { if (DerivesFromGenericType(gt, typeof(Stack<>))) { @@ -5382,14 +5382,14 @@ namespace System.Management.Automation Dbg.Assert(baseType != null, "caller should validate the parameter"); while (derived != null) { - if (derived.GetTypeInfo().IsGenericType) + if (derived.IsGenericType) derived = derived.GetGenericTypeDefinition(); if (derived == baseType) { return true; } - derived = derived.GetTypeInfo().BaseType; + derived = derived.BaseType; } return false; }