Skip to content

Commit

Permalink
Remove some unnecessary GetTypeInfo usage (#44414)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Nov 9, 2020
1 parent 6548832 commit dc3101b
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public int ScalarLength
/// </summary>
public static Func<PropertyValue, PropertyValue> GetPropertyGetter(PropertyInfo property)
{
if (property.DeclaringType!.GetTypeInfo().IsValueType)
if (property.DeclaringType!.IsValueType)
return GetBoxedValueTypePropertyGetter(property);
else
return GetReferenceTypePropertyGetter(property);
Expand All @@ -180,7 +180,7 @@ private static Func<PropertyValue, PropertyValue> GetBoxedValueTypePropertyGette
{
Type type = property.PropertyType;

if (type.GetTypeInfo().IsEnum)
if (type.IsEnum)
type = Enum.GetUnderlyingType(type);

Func<object?, PropertyValue> factory = GetFactory(type);
Expand Down Expand Up @@ -224,7 +224,7 @@ public override Func<PropertyValue, PropertyValue> GetPropertyGetter(PropertyInf
}
else
{
if (type.GetTypeInfo().IsEnum)
if (type.IsEnum)
type = Enum.GetUnderlyingType(type);

if (type == typeof(bool)) { var f = (Func<TContainer, bool>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public NullableTypeInfo(Type type, List<Type> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static string GetFolderPathCore(SpecialFolder folder, SpecialFolderOptio
Func<string, object> 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<Func<string, object>>();
});
createDirectory(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<argument>ILLink</argument>
<argument>IL2070</argument>
<property name="Scope">member</property>
<property name="Target">M:System.Xml.Serialization.ReflectionXmlSerializationReader.FindDefaultConstructor(System.Reflection.TypeInfo)</property>
<property name="Target">M:System.Xml.Serialization.ReflectionXmlSerializationReader.GetDefaultConstructor(System.Type)</property>
</attribute>
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
<argument>ILLink</argument>
Expand Down Expand Up @@ -308,4 +308,4 @@
<property name="Target">M:System.Xml.Xsl.Runtime.XmlExtensionFunction.CanBind</property>
</attribute>
</assembly>
</linker>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>())!;
}

Expand Down Expand Up @@ -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<object>())!;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit dc3101b

Please sign in to comment.