From dc3101bc11eeef183b6d7c3ab38f0a13248c7783 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 9 Nov 2020 16:11:41 -0500 Subject: [PATCH] Remove some unnecessary GetTypeInfo usage (#44414) --- .../src/System/Drawing/Bitmap.Unix.cs | 2 +- .../src/System/Drawing/FontConverter.cs | 2 +- .../src/System/Drawing/Icon.Unix.cs | 4 ++-- .../System/Drawing/ImageFormatConverter.cs | 4 ++-- .../Drawing/ToolboxBitmapAttribute.Unix.cs | 2 +- .../src/System/Drawing/macFunctions.cs | 2 +- .../Tracing/TraceLogging/PropertyValue.cs | 6 ++--- .../Tracing/TraceLogging/SimpleTypeInfos.cs | 2 +- .../Environment.GetFolderPathCore.Unix.cs | 2 +- .../src/System/Xml/Linq/XHelper.cs | 5 +--- .../src/ILLink/ILLink.Suppressions.xml | 4 ++-- .../ReflectionXmlSerializationReader.cs | 24 +++---------------- .../ReflectionXmlSerializationWriter.cs | 4 ++-- .../Security/AccessControl/ObjectSecurity.cs | 4 ++-- .../System/Security/Principal/IRCollection.cs | 2 +- .../src/System/Text/BaseCodePageEncoding.cs | 2 +- 16 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.Unix.cs index 7142f89ae95c03..f35f980d220eff 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.Unix.cs @@ -74,7 +74,7 @@ public Bitmap(Type type, string resource) if (type == null) throw new NullReferenceException(); - Stream? s = type.GetTypeInfo().Assembly.GetManifestResourceStream(type, resource); + Stream? s = type.Assembly.GetManifestResourceStream(type, resource); if (s == null) { string msg = string.Format("Resource '{0}' was not found.", resource); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs index 99a327b2ba5041..ec5d71b1da8a51 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs @@ -87,7 +87,7 @@ public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? c if (destinationType == typeof(InstanceDescriptor)) { - ConstructorInfo? met = typeof(Font).GetTypeInfo().GetConstructor(new Type[] { typeof(string), typeof(float), typeof(FontStyle), typeof(GraphicsUnit) }); + ConstructorInfo? met = typeof(Font).GetConstructor(new Type[] { typeof(string), typeof(float), typeof(FontStyle), typeof(GraphicsUnit) }); object[] args = new object[4]; args[0] = font.Name; args[1] = font.Size; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Unix.cs index 4cb66531a2d2b4..9c4b2ddd921fee 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Unix.cs @@ -247,7 +247,7 @@ public Icon(Type type, string resource) if (type == null) throw new NullReferenceException(); - using (Stream? s = type.GetTypeInfo().Assembly.GetManifestResourceStream(type, resource)) + using (Stream? s = type.Assembly.GetManifestResourceStream(type, resource)) { if (s == null) { @@ -259,7 +259,7 @@ public Icon(Type type, string resource) internal Icon(string resourceName, bool undisposable) { - using (Stream? s = typeof(Icon).GetTypeInfo().Assembly.GetManifestResourceStream(resourceName)) + using (Stream? s = typeof(Icon).Assembly.GetManifestResourceStream(resourceName)) { if (s == null) { diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/ImageFormatConverter.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/ImageFormatConverter.cs index 908477d27beb47..5fc55f1f9fe508 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/ImageFormatConverter.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/ImageFormatConverter.cs @@ -101,11 +101,11 @@ public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? c if (strFormat != null) { - return new InstanceDescriptor(typeof(ImageFormat).GetTypeInfo().GetProperty(strFormat), null); + return new InstanceDescriptor(typeof(ImageFormat).GetProperty(strFormat), null); } else { - ConstructorInfo? ctor = typeof(ImageFormat).GetTypeInfo().GetConstructor(new Type[] { typeof(Guid) }); + ConstructorInfo? ctor = typeof(ImageFormat).GetConstructor(new Type[] { typeof(Guid) }); return new InstanceDescriptor(ctor, new object[] { imgFormat.Guid }); } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/ToolboxBitmapAttribute.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/ToolboxBitmapAttribute.Unix.cs index d3815a01836c29..51708e341e01a3 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/ToolboxBitmapAttribute.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/ToolboxBitmapAttribute.Unix.cs @@ -122,7 +122,7 @@ public override int GetHashCode() try { - using (System.IO.Stream? s = t.GetTypeInfo().Assembly.GetManifestResourceStream(t.Namespace + "." + imageName)) + using (System.IO.Stream? s = t.Assembly.GetManifestResourceStream(t.Namespace + "." + imageName)) { if (s == null) { diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/macFunctions.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/macFunctions.cs index 52ce76b9ee2d58..16523523fb295f 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/macFunctions.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/macFunctions.cs @@ -59,7 +59,7 @@ internal static class MacSupport Type? driver_type = asm.GetType("System.Windows.Forms.XplatUICarbon"); if (driver_type != null) { - return (Delegate?)driver_type.GetTypeInfo().GetField("HwndDelegate", BindingFlags.NonPublic | BindingFlags.Static)!.GetValue(null); + return (Delegate?)driver_type.GetField("HwndDelegate", BindingFlags.NonPublic | BindingFlags.Static)!.GetValue(null); } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs index a40f61930a244e..0e730ab9593143 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs @@ -164,7 +164,7 @@ public int ScalarLength /// public static Func GetPropertyGetter(PropertyInfo property) { - if (property.DeclaringType!.GetTypeInfo().IsValueType) + if (property.DeclaringType!.IsValueType) return GetBoxedValueTypePropertyGetter(property); else return GetReferenceTypePropertyGetter(property); @@ -180,7 +180,7 @@ private static Func GetBoxedValueTypePropertyGette { Type type = property.PropertyType; - if (type.GetTypeInfo().IsEnum) + if (type.IsEnum) type = Enum.GetUnderlyingType(type); Func factory = GetFactory(type); @@ -224,7 +224,7 @@ public override Func GetPropertyGetter(PropertyInf } else { - if (type.GetTypeInfo().IsEnum) + if (type.IsEnum) type = Enum.GetUnderlyingType(type); if (type == typeof(bool)) { var f = (Func)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs index 5b26919f109ce5..e51c6c7aec4d88 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs @@ -275,7 +275,7 @@ public NullableTypeInfo(Type type, List recursionCheck) Type[] typeArgs = type.GenericTypeArguments; Debug.Assert(typeArgs.Length == 1); this.valueInfo = TraceLoggingTypeInfo.GetInstance(typeArgs[0], recursionCheck); - this.valueGetter = PropertyValue.GetPropertyGetter(type.GetTypeInfo().GetDeclaredProperty("Value")!); + this.valueGetter = PropertyValue.GetPropertyGetter(type.GetProperty("Value")!); } public override void WriteMetadata( diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.GetFolderPathCore.Unix.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.GetFolderPathCore.Unix.cs index 10374813d6660d..6e5e7ebc050b3b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.GetFolderPathCore.Unix.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.GetFolderPathCore.Unix.cs @@ -44,7 +44,7 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio Func createDirectory = LazyInitializer.EnsureInitialized(ref s_directoryCreateDirectory, static () => { Type dirType = Type.GetType("System.IO.Directory, System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: true)!; - MethodInfo mi = dirType.GetTypeInfo().GetDeclaredMethod("CreateDirectory")!; + MethodInfo mi = dirType.GetMethod("CreateDirectory", BindingFlags.Public | BindingFlags.Static)!; return mi.CreateDelegate>(); }); createDirectory(path); diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XHelper.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XHelper.cs index f127fe02fa4cb3..dd0b1947bf14d0 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XHelper.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XHelper.cs @@ -13,10 +13,7 @@ internal static bool IsInstanceOfType(object? o, Type type) { Debug.Assert(type != null); - if (o == null) - return false; - - return type.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()); + return o != null && type.IsAssignableFrom(o.GetType()); } } } diff --git a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Suppressions.xml index e9162eedfc80a2..2feabe05d6d392 100644 --- a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Suppressions.xml +++ b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Suppressions.xml @@ -83,7 +83,7 @@ ILLink IL2070 member - M:System.Xml.Serialization.ReflectionXmlSerializationReader.FindDefaultConstructor(System.Reflection.TypeInfo) + M:System.Xml.Serialization.ReflectionXmlSerializationReader.GetDefaultConstructor(System.Type) ILLink @@ -308,4 +308,4 @@ M:System.Xml.Xsl.Runtime.XmlExtensionFunction.CanBind - \ No newline at end of file + diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs index 0458a4e06712b4..92344354915f1f 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs @@ -1295,27 +1295,9 @@ private Hashtable WriteHashtable(EnumMapping mapping, string name) return obj; } - private ConstructorInfo? GetDefaultConstructor(Type type) - { - if (type.IsValueType) - return null; - - ConstructorInfo? ctor = FindDefaultConstructor(type.GetTypeInfo()); - return ctor; - } - - private static ConstructorInfo? FindDefaultConstructor(TypeInfo ti) - { - foreach (ConstructorInfo ci in ti.DeclaredConstructors) - { - if (!ci.IsStatic && ci.GetParameters().Length == 0) - { - return ci; - } - } - - return null; - } + private ConstructorInfo? GetDefaultConstructor(Type type) => + type.IsValueType ? null : + type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null); private object? WriteEncodedStructMethod(StructMapping structMapping) { diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs index 705dfcb0a26737..1bbf66d2ba9405 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs @@ -576,7 +576,7 @@ private void WriteStructMethod(StructMapping mapping, string n, string? ns, obje if (m.CheckShouldPersist) { string methodInvoke = "ShouldSerialize" + m.Name; - MethodInfo method = o!.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke)!; + MethodInfo method = o!.GetType().GetMethod(methodInvoke, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)!; shouldPersist = (bool)method.Invoke(o, Array.Empty())!; } @@ -608,7 +608,7 @@ private void WriteStructMethod(StructMapping mapping, string n, string? ns, obje if (m.CheckShouldPersist) { string methodInvoke = "ShouldSerialize" + m.Name; - MethodInfo method = o!.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke)!; + MethodInfo method = o!.GetType().GetMethod(methodInvoke, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)!; shouldPersist = (bool)method.Invoke(o, Array.Empty())!; } diff --git a/src/libraries/System.Security.AccessControl/src/System/Security/AccessControl/ObjectSecurity.cs b/src/libraries/System.Security.AccessControl/src/System/Security/AccessControl/ObjectSecurity.cs index e49e9d061ae75f..b07608cc5e05ef 100644 --- a/src/libraries/System.Security.AccessControl/src/System/Security/AccessControl/ObjectSecurity.cs +++ b/src/libraries/System.Security.AccessControl/src/System/Security/AccessControl/ObjectSecurity.cs @@ -675,7 +675,7 @@ public virtual bool ModifyAccessRule(AccessControlModification modification, Acc throw new ArgumentNullException(nameof(rule)); } - if (!this.AccessRuleType.GetTypeInfo().IsAssignableFrom(rule.GetType().GetTypeInfo())) + if (!this.AccessRuleType.IsAssignableFrom(rule.GetType())) { throw new ArgumentException( SR.AccessControl_InvalidAccessRuleType, @@ -701,7 +701,7 @@ public virtual bool ModifyAuditRule(AccessControlModification modification, Audi throw new ArgumentNullException(nameof(rule)); } - if (!this.AuditRuleType.GetTypeInfo().IsAssignableFrom(rule.GetType().GetTypeInfo())) + if (!this.AuditRuleType.IsAssignableFrom(rule.GetType())) { throw new ArgumentException( SR.AccessControl_InvalidAuditRuleType, diff --git a/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/IRCollection.cs b/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/IRCollection.cs index 131ff6dbf8319c..4951152f6cd515 100644 --- a/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/IRCollection.cs +++ b/src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/IRCollection.cs @@ -165,7 +165,7 @@ public IdentityReferenceCollection Translate(Type targetType, bool forceSuccess) // Target type must be a subclass of IdentityReference // - if (!targetType.GetTypeInfo().IsSubclassOf(typeof(IdentityReference))) + if (!targetType.IsSubclassOf(typeof(IdentityReference))) { throw new ArgumentException(SR.IdentityReference_MustBeIdentityReference, nameof(targetType)); } diff --git a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/BaseCodePageEncoding.cs b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/BaseCodePageEncoding.cs index 46c8a593d1048b..1a48d4adc31baa 100644 --- a/src/libraries/System.Text.Encoding.CodePages/src/System/Text/BaseCodePageEncoding.cs +++ b/src/libraries/System.Text.Encoding.CodePages/src/System/Text/BaseCodePageEncoding.cs @@ -157,7 +157,7 @@ internal static Stream GetEncodingDataStream(string tableName) // NOTE: We must reflect on a public type that is exposed in the contract here // (i.e. CodePagesEncodingProvider), otherwise we will not get a reference to // the right assembly. - Stream? stream = typeof(CodePagesEncodingProvider).GetTypeInfo().Assembly.GetManifestResourceStream(tableName); + Stream? stream = typeof(CodePagesEncodingProvider).Assembly.GetManifestResourceStream(tableName); if (stream == null) {