Skip to content

Commit

Permalink
Remove unnecessary calls to GetTypeInfo (#27974)
Browse files Browse the repository at this point in the history
  • Loading branch information
eerhardt authored Nov 25, 2020
1 parent 77fe36f commit d7187b0
Show file tree
Hide file tree
Showing 88 changed files with 216 additions and 227 deletions.
2 changes: 1 addition & 1 deletion src/Components/Server/src/BlazorPack/SequenceOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private class SequenceSegment : ReadOnlySequenceSegment<T>
/// <summary>
/// A value indicating whether the element is a value type.
/// </summary>
private static readonly bool IsValueTypeElement = typeof(T).GetTypeInfo().IsValueType;
private static readonly bool IsValueTypeElement = typeof(T).IsValueType;

/// <summary>
/// Gets the backing array, when using an <see cref="ArrayPool{T}"/> instead of a <see cref="MemoryPool{T}"/>.
Expand Down
6 changes: 3 additions & 3 deletions src/Components/WebAssembly/Sdk/tools/Application.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -71,7 +71,7 @@ public Application(

private string GetInformationalVersion()
{
var assembly = typeof(Application).GetTypeInfo().Assembly;
var assembly = typeof(Application).Assembly;
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return attribute.InformationalVersion;
}
Expand All @@ -95,4 +95,4 @@ private static string[] ExpandResponseFiles(string[] args)
return expandedArgs.ToArray();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static class Program
var compilation = CSharpCompilation.Create(
$"TestAssembly-{Guid.NewGuid().ToString("D")}",
new[] { syntaxTree },
new[] { MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location) },
new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) },
new CSharpCompilationOptions(OutputKind.ConsoleApplication));
using var ms = new MemoryStream();
var compilationResult = compilation.Emit(ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ private void CreateInstance_ForwardsAcrossVersionChangesImpl(Version newVersion)
{
var activator = new TypeForwardingActivator(null);

var typeInfo = typeof(ClassWithParameterlessCtor).GetTypeInfo();
var typeName = typeInfo.FullName;
var assemblyName = typeInfo.Assembly.GetName();
var type = typeof(ClassWithParameterlessCtor);
var typeName = type.FullName;
var assemblyName = type.Assembly.GetName();

assemblyName.Version = newVersion;
var newName = $"{typeName}, {assemblyName}";

Assert.NotEqual(typeInfo.AssemblyQualifiedName, newName);
Assert.NotEqual(type.AssemblyQualifiedName, newName);
Assert.IsType<ClassWithParameterlessCtor>(activator.CreateInstance(typeof(object), newName, out var forwarded));
Assert.True(forwarded, "Should have forwarded this type to new version or namespace");
}
Expand Down
4 changes: 2 additions & 2 deletions src/DefaultBuilder/src/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static IWebHost Start(RequestDelegate app) =>
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
public static IWebHost Start(string url, RequestDelegate app)
{
var startupAssemblyName = app.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name;
var startupAssemblyName = app.GetMethodInfo().DeclaringType!.Assembly.GetName().Name;
return StartWith(url: url, configureServices: null, app: appBuilder => appBuilder.Run(app), applicationName: startupAssemblyName);
}

Expand All @@ -64,7 +64,7 @@ public static IWebHost Start(Action<IRouteBuilder> routeBuilder) =>
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
public static IWebHost Start(string url, Action<IRouteBuilder> routeBuilder)
{
var startupAssemblyName = routeBuilder.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name;
var startupAssemblyName = routeBuilder.GetMethodInfo().DeclaringType!.Assembly.GetName().Name;
return StartWith(url, services => services.AddRouting(), appBuilder => appBuilder.UseRouter(routeBuilder), applicationName: startupAssemblyName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override object ReadJson(
return null;
}

var genericType = objectType.GetTypeInfo().GenericTypeArguments[0];
var genericType = objectType.GenericTypeArguments[0];

// load jObject
var jObject = JArray.Load(reader);
Expand Down Expand Up @@ -61,4 +61,4 @@ public override object ReadJson(
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -63,11 +63,10 @@ public static ConversionResult CopyTo(object value, Type typeToConvertTo)

private static bool IsNullableType(Type type)
{
var typeInfo = type.GetTypeInfo();
if (typeInfo.IsValueType)
if (type.IsValueType)
{
// value types are only nullable if they are Nullable<T>
return typeInfo.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/Features/JsonPatch/src/Internal/DynamicObjectAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -66,7 +66,7 @@ public virtual bool TryRemove(
// Setting the value to "null" will use the default value in case of value types, and
// null in case of reference types
object value = null;
if (property.GetType().GetTypeInfo().IsValueType
if (property.GetType().IsValueType
&& Nullable.GetUnderlyingType(property.GetType()) == null)
{
value = Activator.CreateInstance(property.GetType());
Expand Down
4 changes: 2 additions & 2 deletions src/Features/JsonPatch/src/Internal/PocoAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -94,7 +94,7 @@ public virtual bool TryRemove(
// Setting the value to "null" will use the default value in case of value types, and
// null in case of reference types
object value = null;
if (jsonProperty.PropertyType.GetTypeInfo().IsValueType
if (jsonProperty.PropertyType.IsValueType
&& Nullable.GetUnderlyingType(jsonProperty.PropertyType) == null)
{
value = Activator.CreateInstance(jsonProperty.PropertyType);
Expand Down
22 changes: 11 additions & 11 deletions src/FileProviders/Embedded/test/EmbeddedFileProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void ConstructorWithNullAssemblyThrowsArgumentException()
public void GetFileInfo_ReturnsNotFoundFileInfo_IfFileDoesNotExist()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly);
var provider = new EmbeddedFileProvider(GetType().Assembly);

// Act
var fileInfo = provider.GetFileInfo("DoesNotExist.Txt");
Expand All @@ -39,7 +39,7 @@ public void GetFileInfo_ReturnsNotFoundFileInfo_IfFileDoesNotExist()
public void GetFileInfo_ReturnsFilesAtRoot(string filePath)
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly);
var provider = new EmbeddedFileProvider(GetType().Assembly);
var expectedFileLength = 8;

// Act
Expand All @@ -59,7 +59,7 @@ public void GetFileInfo_ReturnsFilesAtRoot(string filePath)
public void GetFileInfo_ReturnsNotFoundFileInfo_IfFileDoesNotExistUnderSpecifiedNamespace()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace + ".SubNamespace");
var provider = new EmbeddedFileProvider(GetType().Assembly, Namespace + ".SubNamespace");

// Act
var fileInfo = provider.GetFileInfo("File.txt");
Expand All @@ -73,7 +73,7 @@ public void GetFileInfo_ReturnsNotFoundFileInfo_IfFileDoesNotExistUnderSpecified
public void GetFileInfo_ReturnsNotFoundIfPathStartsWithBackSlash()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly);
var provider = new EmbeddedFileProvider(GetType().Assembly);

// Act
var fileInfo = provider.GetFileInfo("\\File.txt");
Expand Down Expand Up @@ -106,7 +106,7 @@ public static TheoryData GetFileInfo_LocatesFilesUnderSpecifiedNamespaceData
public void GetFileInfo_LocatesFilesUnderSpecifiedNamespace(string path)
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace + ".Resources");
var provider = new EmbeddedFileProvider(GetType().Assembly, Namespace + ".Resources");

// Act
var fileInfo = provider.GetFileInfo(path);
Expand Down Expand Up @@ -144,7 +144,7 @@ public static TheoryData GetFileInfo_LocatesFilesUnderSubDirectoriesData
public void GetFileInfo_LocatesFilesUnderSubDirectories(string path)
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly);
var provider = new EmbeddedFileProvider(GetType().Assembly);

// Act
var fileInfo = provider.GetFileInfo(path);
Expand All @@ -165,7 +165,7 @@ public void GetFileInfo_LocatesFilesUnderSubDirectories(string path)
public void GetDirectoryContents_ReturnsAllFilesInFileSystem(string path)
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace + ".Resources");
var provider = new EmbeddedFileProvider(GetType().Assembly, Namespace + ".Resources");

// Act
var files = provider.GetDirectoryContents(path);
Expand All @@ -185,7 +185,7 @@ public void GetDirectoryContents_ReturnsAllFilesInFileSystem(string path)
public void GetDirectoryContents_ReturnsEmptySequence_IfResourcesDoNotExistUnderNamespace()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, "Unknown.Namespace");
var provider = new EmbeddedFileProvider(GetType().Assembly, "Unknown.Namespace");

// Act
var files = provider.GetDirectoryContents(string.Empty);
Expand All @@ -202,7 +202,7 @@ public void GetDirectoryContents_ReturnsEmptySequence_IfResourcesDoNotExistUnder
public void GetDirectoryContents_ReturnsNotFoundDirectoryContents_IfHierarchicalPathIsSpecified(string path)
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly);
var provider = new EmbeddedFileProvider(GetType().Assembly);

// Act
var files = provider.GetDirectoryContents(path);
Expand All @@ -217,7 +217,7 @@ public void GetDirectoryContents_ReturnsNotFoundDirectoryContents_IfHierarchical
public void Watch_ReturnsNoOpTrigger()
{
// Arange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly);
var provider = new EmbeddedFileProvider(GetType().Assembly);

// Act
var token = provider.Watch("Resources/File.txt");
Expand All @@ -228,4 +228,4 @@ public void Watch_ReturnsNoOpTrigger()
Assert.False(token.HasChanged);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Grpc/test/testassets/InteropClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void Main(string[] args)
{
Console.WriteLine("Application started.");

var runtimeVersion = typeof(object).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
var runtimeVersion = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
Console.WriteLine($"NetCoreAppVersion: {runtimeVersion}");

InteropClient.Run(args);
Expand Down
4 changes: 2 additions & 2 deletions src/Grpc/test/testassets/InteropWebsite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicat
{
Console.WriteLine("Application started.");

var runtimeVersion = typeof(object).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
var runtimeVersion = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
Console.WriteLine($"NetCoreAppVersion: {runtimeVersion}");
var aspNetCoreVersion = typeof(HeaderNames).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
var aspNetCoreVersion = typeof(HeaderNames).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
Console.WriteLine($"AspNetCoreAppVersion: {aspNetCoreVersion}");
});

Expand Down
2 changes: 1 addition & 1 deletion src/Hosting/Abstractions/src/HostingStartupAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public HostingStartupAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMe
throw new ArgumentNullException(nameof(hostingStartupType));
}

if (!typeof(IHostingStartup).GetTypeInfo().IsAssignableFrom(hostingStartupType.GetTypeInfo()))
if (!typeof(IHostingStartup).IsAssignableFrom(hostingStartupType))
{
throw new ArgumentException($@"""{hostingStartupType}"" does not implement {typeof(IHostingStartup)}.", nameof(hostingStartupType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ private RequestDelegate BuildErrorPageApplication(Exception exception)
{
RuntimeDisplayName = RuntimeInformation.FrameworkDescription
};
var systemRuntimeAssembly = typeof(System.ComponentModel.DefaultValueAttribute).GetTypeInfo().Assembly;
var systemRuntimeAssembly = typeof(System.ComponentModel.DefaultValueAttribute).Assembly;
var assemblyVersion = new AssemblyName(systemRuntimeAssembly.FullName).Version.ToString();
var clrVersion = assemblyVersion;
model.RuntimeArchitecture = RuntimeInformation.ProcessArchitecture.ToString();
var currentAssembly = typeof(ErrorPage).GetTypeInfo().Assembly;
var currentAssembly = typeof(ErrorPage).Assembly;
model.CurrentAssemblyVesion = currentAssembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
Expand Down
4 changes: 2 additions & 2 deletions src/Hosting/Hosting/src/Internal/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ private RequestDelegate BuildApplication()
{
RuntimeDisplayName = RuntimeInformation.FrameworkDescription
};
var systemRuntimeAssembly = typeof(System.ComponentModel.DefaultValueAttribute).GetTypeInfo().Assembly;
var systemRuntimeAssembly = typeof(System.ComponentModel.DefaultValueAttribute).Assembly;
var assemblyVersion = new AssemblyName(systemRuntimeAssembly.FullName).Version.ToString();
var clrVersion = assemblyVersion;
model.RuntimeArchitecture = RuntimeInformation.ProcessArchitecture.ToString();
var currentAssembly = typeof(ErrorPage).GetTypeInfo().Assembly;
var currentAssembly = typeof(ErrorPage).Assembly;
model.CurrentAssemblyVesion = currentAssembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
Expand Down
2 changes: 1 addition & 1 deletion src/Hosting/Hosting/src/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private IServiceCollection BuildCommonServices(out AggregateException? hostingSt
{
var startupType = StartupLoader.FindStartupType(_options.StartupAssembly, _hostingEnvironment.EnvironmentName);

if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
if (typeof(IStartup).IsAssignableFrom(startupType))
{
services.AddSingleton(typeof(IStartup), startupType);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Hosting/Hosting/src/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class WebHostBuilderExtensions
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<IApplicationBuilder> configureApp)
{
return hostBuilder.Configure((_, app) => configureApp(app), configureApp.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name!);
return hostBuilder.Configure((_, app) => configureApp(app), configureApp.GetMethodInfo().DeclaringType!.Assembly.GetName().Name!);
}

/// <summary>
Expand All @@ -41,7 +41,7 @@ public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, IApplicationBuilder> configureApp)
{
return hostBuilder.Configure(configureApp, configureApp.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name!);
return hostBuilder.Configure(configureApp, configureApp.GetMethodInfo().DeclaringType!.Assembly.GetName().Name!);
}

private static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, IApplicationBuilder> configureApp, string startupAssemblyName)
Expand Down Expand Up @@ -82,7 +82,7 @@ private static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Actio
throw new ArgumentNullException(nameof(startupFactory));
}

var startupAssemblyName = startupFactory.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name;
var startupAssemblyName = startupFactory.GetMethodInfo().DeclaringType!.Assembly.GetName().Name;

hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);

Expand Down Expand Up @@ -125,7 +125,7 @@ public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, [Dyna
throw new ArgumentNullException(nameof(startupType));
}

var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name;
var startupAssemblyName = startupType.Assembly.GetName().Name;

hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);

Expand All @@ -138,7 +138,7 @@ public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, [Dyna
return hostBuilder
.ConfigureServices(services =>
{
if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
if (typeof(IStartup).IsAssignableFrom(startupType))
{
services.AddSingleton(typeof(IStartup), startupType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static class UseMiddlewareExtensions
/// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, [DynamicallyAccessedMembers(MiddlewareAccessibility)] Type middleware, params object?[] args)
{
if (typeof(IMiddleware).GetTypeInfo().IsAssignableFrom(middleware.GetTypeInfo()))
if (typeof(IMiddleware).IsAssignableFrom(middleware))
{
// IMiddleware doesn't support passing args directly since it's
// activated from the container
Expand Down
Loading

0 comments on commit d7187b0

Please sign in to comment.