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.
This commit is contained in:
Dongbo Wang 2018-04-13 15:10:10 -07:00 committed by GitHub
parent f9f1bd29ad
commit 11631e7412
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 39 additions and 44 deletions

View file

@ -185,7 +185,7 @@ namespace System.Management.Automation
} }
else else
{ {
bool isNullable = boolType.GetTypeInfo().IsGenericType && bool isNullable = boolType.IsGenericType &&
boolType.GetGenericTypeDefinition() == typeof(Nullable<>); boolType.GetGenericTypeDefinition() == typeof(Nullable<>);
if (!isNullable && LanguagePrimitives.IsBooleanType(boolType)) if (!isNullable && LanguagePrimitives.IsBooleanType(boolType))

View file

@ -340,7 +340,7 @@ namespace System.Management.Automation
if (ImplementingType != null) 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; OutputTypeAttribute attr = (OutputTypeAttribute)o;
_outputType.AddRange(attr.Type); _outputType.AddRange(attr.Type);

View file

@ -697,7 +697,7 @@ namespace System.Management.Automation
// Process the attributes on the cmdlet // Process the attributes on the cmdlet
var customAttributes = CommandType.GetTypeInfo().GetCustomAttributes(false); var customAttributes = CommandType.GetCustomAttributes(false);
foreach (Attribute attribute in customAttributes) foreach (Attribute attribute in customAttributes)
{ {

View file

@ -613,7 +613,6 @@ namespace System.Management.Automation
{ {
ParameterCollectionType = ParameterCollectionType.NotCollection; ParameterCollectionType = ParameterCollectionType.NotCollection;
Diagnostics.Assert(type != null, "Caller to verify type argument"); Diagnostics.Assert(type != null, "Caller to verify type argument");
TypeInfo typeInfo = type.GetTypeInfo();
// NTRAID#Windows OS Bugs-1009284-2004/05/11-JeffJon // NTRAID#Windows OS Bugs-1009284-2004/05/11-JeffJon
// What other collection types should be supported? // What other collection types should be supported?
@ -636,8 +635,8 @@ namespace System.Management.Automation
} }
Type[] interfaces = type.GetInterfaces(); Type[] interfaces = type.GetInterfaces();
if (interfaces.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)) if (interfaces.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>))
|| (typeInfo.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>))) || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
{ {
return; return;
} }
@ -648,7 +647,7 @@ namespace System.Management.Automation
// is more efficient to bind than ICollection<T>. This optimization // is more efficient to bind than ICollection<T>. This optimization
// retrieves the element type so that we can coerce the elements. // retrieves the element type so that we can coerce the elements.
// Otherwise they must already be the right type. // 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; ParameterCollectionType = ParameterCollectionType.IList;
// figure out elementType // figure out elementType
@ -666,7 +665,7 @@ namespace System.Management.Automation
// to an ICollection<T> is via reflected calls to Add(T), // to an ICollection<T> is via reflected calls to Add(T),
// but the advantage over plain IList is that we can typecast the elements. // but the advantage over plain IList is that we can typecast the elements.
Type interfaceICollection = Type interfaceICollection =
interfaces.FirstOrDefault(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>)); interfaces.FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection<>));
if (interfaceICollection != null) if (interfaceICollection != null)
{ {
// We only deal with the first type for which ICollection<T> is implemented // We only deal with the first type for which ICollection<T> is implemented

View file

@ -26,7 +26,7 @@ namespace System.Management.Automation
/// </param> /// </param>
public FlagsExpression(string expression) public FlagsExpression(string expression)
{ {
if (!typeof(T).GetTypeInfo().IsEnum) if (!typeof(T).IsEnum)
{ {
throw InterpreterError.NewInterpreterException(expression, typeof(RuntimeException), throw InterpreterError.NewInterpreterException(expression, typeof(RuntimeException),
null, "InvalidGenericType", EnumExpressionEvaluatorStrings.InvalidGenericType); null, "InvalidGenericType", EnumExpressionEvaluatorStrings.InvalidGenericType);
@ -58,7 +58,7 @@ namespace System.Management.Automation
/// </param> /// </param>
public FlagsExpression(object[] expression) public FlagsExpression(object[] expression)
{ {
if (!typeof(T).GetTypeInfo().IsEnum) if (!typeof(T).IsEnum)
{ {
throw InterpreterError.NewInterpreterException(expression, typeof(RuntimeException), throw InterpreterError.NewInterpreterException(expression, typeof(RuntimeException),
null, "InvalidGenericType", EnumExpressionEvaluatorStrings.InvalidGenericType); null, "InvalidGenericType", EnumExpressionEvaluatorStrings.InvalidGenericType);

View file

@ -1464,7 +1464,7 @@ namespace System.Management.Automation
methodContents.Emit(OpCodes.Ldarg, counter); methodContents.Emit(OpCodes.Ldarg, counter);
// Box the value type if necessary // Box the value type if necessary
if (parameterTypes[counter - 1].GetTypeInfo().IsValueType) if (parameterTypes[counter - 1].IsValueType)
{ {
methodContents.Emit(OpCodes.Box, parameterTypes[counter - 1]); methodContents.Emit(OpCodes.Box, parameterTypes[counter - 1]);
} }

View file

@ -1472,8 +1472,8 @@ namespace Microsoft.PowerShell.Commands
private static PSObject GetParameterType(Type parameterType) private static PSObject GetParameterType(Type parameterType)
{ {
PSObject returnParameterType = new PSObject(); PSObject returnParameterType = new PSObject();
bool isEnum = parameterType.GetTypeInfo().IsEnum; bool isEnum = parameterType.IsEnum;
bool isArray = parameterType.GetTypeInfo().IsArray; bool isArray = parameterType.IsArray;
returnParameterType.Properties.Add(new PSNoteProperty("FullName", parameterType.FullName)); returnParameterType.Properties.Add(new PSNoteProperty("FullName", parameterType.FullName));
returnParameterType.Properties.Add(new PSNoteProperty("IsEnum", isEnum)); returnParameterType.Properties.Add(new PSNoteProperty("IsEnum", isEnum));
returnParameterType.Properties.Add(new PSNoteProperty("IsArray", isArray)); returnParameterType.Properties.Add(new PSNoteProperty("IsArray", isArray));
@ -1483,7 +1483,7 @@ namespace Microsoft.PowerShell.Commands
returnParameterType.Properties.Add(new PSNoteProperty("EnumValues", enumValues)); returnParameterType.Properties.Add(new PSNoteProperty("EnumValues", enumValues));
bool hasFlagAttribute = (isArray) ? 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)); returnParameterType.Properties.Add(new PSNoteProperty("HasFlagAttribute", hasFlagAttribute));
// Recurse into array elements. // Recurse into array elements.

View file

@ -4976,9 +4976,9 @@ if($paths) {
return assembly; return assembly;
} }
private static T GetCustomAttribute<T>(TypeInfo decoratedType) where T : Attribute private static T GetCustomAttribute<T>(Type decoratedType) where T : Attribute
{ {
var attributes = CustomAttributeExtensions.GetCustomAttributes<T>(decoratedType, false); var attributes = decoratedType.GetCustomAttributes<T>(false);
var customAttrs = attributes.ToArray(); var customAttrs = attributes.ToArray();
Debug.Assert(customAttrs.Length <= 1, "CmdletAttribute and/or CmdletProviderAttribute cannot normally appear more than once"); 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) foreach (Type type in assemblyTypes)
{ {
var typeInfo = type.GetTypeInfo(); if (!(type.IsPublic || type.IsNestedPublic) || type.IsAbstract)
if (!(typeInfo.IsPublic || typeInfo.IsNestedPublic) || typeInfo.IsAbstract)
continue; continue;
// Check for cmdlets // Check for cmdlets
@ -5247,7 +5246,7 @@ if($paths) {
{ {
randomCmdletToCheckLinkDemand = type; randomCmdletToCheckLinkDemand = type;
CmdletAttribute cmdletAttribute = GetCustomAttribute<CmdletAttribute>(typeInfo); CmdletAttribute cmdletAttribute = GetCustomAttribute<CmdletAttribute>(type);
if (cmdletAttribute == null) if (cmdletAttribute == null)
{ {
continue; continue;
@ -5275,7 +5274,7 @@ if($paths) {
} }
cmdlets.Add(cmdletName, cmdlet); cmdlets.Add(cmdletName, cmdlet);
var aliasAttribute = GetCustomAttribute<AliasAttribute>(typeInfo); var aliasAttribute = GetCustomAttribute<AliasAttribute>(type);
if (aliasAttribute != null) if (aliasAttribute != null)
{ {
if (aliases == null) if (aliases == null)
@ -5309,7 +5308,7 @@ if($paths) {
{ {
randomProviderToCheckLinkDemand = type; randomProviderToCheckLinkDemand = type;
CmdletProviderAttribute providerAttribute = GetCustomAttribute<CmdletProviderAttribute>(typeInfo); CmdletProviderAttribute providerAttribute = GetCustomAttribute<CmdletProviderAttribute>(type);
if (providerAttribute == null) if (providerAttribute == null)
{ {
continue; continue;
@ -5453,8 +5452,7 @@ if($paths) {
for (int i = 0; i < assemblyTypes.Length; i++) for (int i = 0; i < assemblyTypes.Length; i++)
{ {
Type type = assemblyTypes[i]; Type type = assemblyTypes[i];
TypeInfo typeInfo = type.GetTypeInfo(); if (!(type.IsPublic || type.IsNestedPublic) || type.IsAbstract) { continue; }
if (!(typeInfo.IsPublic || typeInfo.IsNestedPublic) || typeInfo.IsAbstract) { continue; }
if (isModuleLoad && typeof(IModuleAssemblyInitializer).IsAssignableFrom(type) && type != typeof(IModuleAssemblyInitializer)) if (isModuleLoad && typeof(IModuleAssemblyInitializer).IsAssignableFrom(type) && type != typeof(IModuleAssemblyInitializer))
{ {

View file

@ -444,7 +444,7 @@ namespace System.Management.Automation
} }
CmdletAttribute ca = null; CmdletAttribute ca = null;
foreach (var attr in cmdletType.GetTypeInfo().GetCustomAttributes(true)) foreach (var attr in cmdletType.GetCustomAttributes(true))
{ {
ca = attr as CmdletAttribute; ca = attr as CmdletAttribute;
if (ca != null) if (ca != null)

View file

@ -2407,8 +2407,7 @@ namespace Microsoft.PowerShell
return String.Empty; return String.Empty;
string result; string result;
TypeInfo typeinfo = type.GetTypeInfo(); if (type.IsGenericType && !type.IsGenericTypeDefinition)
if (typeinfo.IsGenericType && !typeinfo.IsGenericTypeDefinition)
{ {
string genericDefinition = Type(type.GetGenericTypeDefinition(), dropNamespaces); string genericDefinition = Type(type.GetGenericTypeDefinition(), dropNamespaces);
// For regular generic types, we find the backtick character, for example: // 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: // 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[TKey, TValue] ->
// System.Collections.Generic.Dictionary`2+Enumerator[string,string] // 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); var sb = new StringBuilder(genericDefinition, 0, backtickOrLeftBracketIndex, 512);
AddGenericArguments(sb, type.GetGenericArguments(), dropNamespaces); AddGenericArguments(sb, type.GetGenericArguments(), dropNamespaces);
result = sb.ToString(); result = sb.ToString();
} }
else if (typeinfo.IsArray) else if (type.IsArray)
{ {
string elementDefinition = Type(type.GetElementType(), dropNamespaces); string elementDefinition = Type(type.GetElementType(), dropNamespaces);
var sb = new StringBuilder(elementDefinition, elementDefinition.Length + 10); var sb = new StringBuilder(elementDefinition, elementDefinition.Length + 10);
@ -2445,7 +2444,7 @@ namespace Microsoft.PowerShell
} }
if (dropNamespaces) if (dropNamespaces)
{ {
if (typeinfo.IsNested) if (type.IsNested)
{ {
// For nested types, we should return OuterType+InnerType. For example, // For nested types, we should return OuterType+InnerType. For example,
// System.Environment+SpecialFolder -> Environment+SpecialFolder // System.Environment+SpecialFolder -> Environment+SpecialFolder
@ -2468,10 +2467,10 @@ namespace Microsoft.PowerShell
// We can't round trip anything with a generic parameter. // We can't round trip anything with a generic parameter.
// We also can't round trip if we're dropping the namespace. // We also can't round trip if we're dropping the namespace.
if (!typeinfo.IsGenericParameter if (!type.IsGenericParameter
&& !typeinfo.ContainsGenericParameters && !type.ContainsGenericParameters
&& !dropNamespaces && !dropNamespaces
&& !typeinfo.Assembly.GetCustomAttributes(typeof(DynamicClassImplementationAssemblyAttribute)).Any()) && !type.Assembly.GetCustomAttributes(typeof(DynamicClassImplementationAssemblyAttribute)).Any())
{ {
Type roundTripType; Type roundTripType;
TypeResolver.TryResolveType(result, out roundTripType); TypeResolver.TryResolveType(result, out roundTripType);

View file

@ -850,7 +850,7 @@ namespace System.Management.Automation
{ {
return parameterType == null || return parameterType == null ||
isDefaultValue || isDefaultValue ||
(!parameterType.GetTypeInfo().IsValueType && (!parameterType.IsValueType &&
parameterType != typeof(string)); 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 // we don't want to attempt to bind a collection to a scalar unless
// the parameter type is Object or PSObject or enum. // the parameter type is Object or PSObject or enum.
TypeInfo toTypeInfo = toType.GetTypeInfo();
if (GetIList(currentValue) != null && if (GetIList(currentValue) != null &&
toType != typeof(Object) && toType != typeof(Object) &&
toType != typeof(PSObject) && toType != typeof(PSObject) &&
toType != typeof(PSListModifier) && toType != typeof(PSListModifier) &&
(!toTypeInfo.IsGenericType || toTypeInfo.GetGenericTypeDefinition() != typeof(PSListModifier<>)) && (!toType.IsGenericType || toType.GetGenericTypeDefinition() != typeof(PSListModifier<>)) &&
(!toTypeInfo.IsGenericType || toTypeInfo.GetGenericTypeDefinition() != typeof(FlagsExpression<>)) && (!toType.IsGenericType || toType.GetGenericTypeDefinition() != typeof(FlagsExpression<>)) &&
!toTypeInfo.IsEnum) !toType.IsEnum)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }

View file

@ -242,7 +242,7 @@ namespace System.Management.Automation
var propertyExpr = GetPropertyOrFieldExpr(type, property, Expression.Convert(target, type)); var propertyExpr = GetPropertyOrFieldExpr(type, property, Expression.Convert(target, type));
Expression expr = Expression.Assign(propertyExpr, Expression.Convert(value, propertyExpr.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 = var throwInvalidCastExceptionExpr =
Expression.Call(Language.CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException, Expression.Call(Language.CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException,

View file

@ -2285,7 +2285,7 @@ namespace System.Management.Automation
{ {
mi = providerType.GetMethod("GetChildItemsDynamicParameters", mi = providerType.GetMethod("GetChildItemsDynamicParameters",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
providerType = providerType.GetTypeInfo().BaseType; providerType = providerType.BaseType;
} while ( } while (
(mi == null) && (mi == null) &&
(providerType != null) && (providerType != null) &&

View file

@ -279,7 +279,7 @@ namespace System.Management.Automation
Collection<string> types = new Collection<String>(); Collection<string> types = new Collection<String>();
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); types.Add(type.FullName);
} }

View file

@ -256,7 +256,7 @@ namespace System.Management.Automation
if (String.IsNullOrEmpty(resourceId)) if (String.IsNullOrEmpty(resourceId))
throw PSTraceSource.NewArgumentNullException("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; string retValue = null;
try try

View file

@ -5308,7 +5308,7 @@ namespace System.Management.Automation
else else
{ {
Type gt = source.GetType(); Type gt = source.GetType();
if (gt.GetTypeInfo().IsGenericType) if (gt.IsGenericType)
{ {
if (DerivesFromGenericType(gt, typeof(Stack<>))) if (DerivesFromGenericType(gt, typeof(Stack<>)))
{ {
@ -5382,14 +5382,14 @@ namespace System.Management.Automation
Dbg.Assert(baseType != null, "caller should validate the parameter"); Dbg.Assert(baseType != null, "caller should validate the parameter");
while (derived != null) while (derived != null)
{ {
if (derived.GetTypeInfo().IsGenericType) if (derived.IsGenericType)
derived = derived.GetGenericTypeDefinition(); derived = derived.GetGenericTypeDefinition();
if (derived == baseType) if (derived == baseType)
{ {
return true; return true;
} }
derived = derived.GetTypeInfo().BaseType; derived = derived.BaseType;
} }
return false; return false;
} }