提交 57947d20 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #18024 from CyrusNajmabadi/lessHardcoding

Use less hardcoding of type-names when possible.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
......@@ -96,10 +98,10 @@ private bool TryGetMethodReturnType(SyntaxNode node, SemanticModel model, Cancel
private bool IsCorrectTypeForYieldReturn(ITypeSymbol typeArgument, ITypeSymbol returnExpressionType, ITypeSymbol methodReturnType, SemanticModel model)
{
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.IEnumerable");
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.IEnumerator");
var ienumerableGenericSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerable`1");
var ienumeratorGenericSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerator`1");
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator).FullName);
var ienumerableGenericSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable<>).FullName);
var ienumeratorGenericSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator<>).FullName);
if (ienumerableGenericSymbol == null ||
ienumerableSymbol == null ||
......@@ -168,8 +170,8 @@ private bool CanConvertTypes(ITypeSymbol typeArgument, ITypeSymbol returnExpress
private bool IsCorrectTypeForYieldReturn(ITypeSymbol returnExpressionType, ITypeSymbol methodReturnType, SemanticModel model)
{
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.IEnumerable");
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.IEnumerator");
var ienumerableSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
var ienumeratorSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerator).FullName);
if (ienumerableSymbol == null ||
ienumeratorSymbol == null)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
......@@ -105,8 +107,8 @@ protected override async Task<CodeAction> GetCodeFixAsync(SyntaxNode root, Synta
private static bool TryGetIEnumerableSymbols(SemanticModel model, out INamedTypeSymbol ienumerableSymbol, out INamedTypeSymbol ienumerableGenericSymbol)
{
ienumerableSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.IEnumerable");
ienumerableGenericSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerable`1");
ienumerableSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
ienumerableGenericSymbol = model.Compilation.GetTypeByMetadataName(typeof(IEnumerable<>).FullName);
if (ienumerableGenericSymbol == null ||
ienumerableSymbol == null)
......
......@@ -142,10 +142,11 @@ private ITypeSymbol UnwrapType(ITypeSymbol type, Compilation compilation)
return UnwrapType(namedType.TypeArguments[0], compilation);
}
var taskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
var valueTaskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.ValueTask`1");
if (originalDefinition == taskOfTType
|| originalDefinition == valueTaskType)
var taskOfTType = compilation.TaskOfTType();
var valueTaskType = compilation.ValueTaskOfTType();
if (originalDefinition == taskOfTType ||
originalDefinition == valueTaskType)
{
return UnwrapType(namedType.TypeArguments[0], compilation);
}
......
......@@ -3,6 +3,7 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
......@@ -46,7 +47,7 @@ protected override void InitializeWorker(AnalysisContext context)
context.RegisterCompilationStartAction(compilationContext =>
{
var compilation = compilationContext.Compilation;
var expressionTypeOpt = compilation.GetTypeByMetadataName("System.Linq.Expressions.Expression`1");
var expressionTypeOpt = compilation.GetTypeByMetadataName(typeof(Expression<>).FullName);
compilationContext.RegisterSyntaxNodeAction(
syntaxContext => AnalyzeSyntaxNode(syntaxContext, expressionTypeOpt), SyntaxKind.Argument);
});
......
......@@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CodeFixes.Async
{
......@@ -42,7 +43,7 @@ internal abstract partial class AbstractAddAwaitCodeFixProvider : AbstractAsyncC
protected static bool TryGetTaskType(SemanticModel semanticModel, out INamedTypeSymbol taskType)
{
var compilation = semanticModel.Compilation;
taskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
taskType = compilation.TaskType();
return taskType != null;
}
......
......@@ -2550,7 +2550,7 @@ private static bool HasBackingField(IEventSymbol @event)
if (layoutAttribute == null)
{
layoutAttribute = model.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.StructLayoutAttribute");
layoutAttribute = model.Compilation.GetTypeByMetadataName(typeof(StructLayoutAttribute).FullName);
if (layoutAttribute == null)
{
return false;
......
......@@ -544,7 +544,7 @@ private bool WellKnownFrameworkValueType(Compilation compilation, ITypeSymbol ty
return false;
}
var cancellationTokenType = compilation.GetTypeByMetadataName("System.Threading.CancellationToken");
var cancellationTokenType = compilation.GetTypeByMetadataName(typeof(CancellationToken).FullName);
if (cancellationTokenType != null && cancellationTokenType.Equals(type))
{
return true;
......
......@@ -75,9 +75,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
private (INamedTypeSymbol taskType, INamedTypeSymbol taskOfTType, INamedTypeSymbol valueTaskOfTTypeOpt) GetTaskTypes(Compilation compilation)
{
var taskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
var taskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
var valueTaskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.ValueTask`1");
var taskType = compilation.TaskType();
var taskOfTType = compilation.TaskOfTType();
var valueTaskOfTType = compilation.ValueTaskOfTType();
return (taskType, taskOfTType, valueTaskOfTType);
}
......
......@@ -88,8 +88,8 @@ private async Task<Solution> RenameThenRemoveAsyncTokenAsync(Document document,
Document document, IMethodSymbol methodSymbolOpt, SyntaxNode node, CancellationToken cancellationToken)
{
var compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
var taskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
var taskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
var taskType = compilation.TaskType();
var taskOfTType = compilation.TaskOfTType();
var annotation = new SyntaxAnnotation();
var newNode = RemoveAsyncTokenAndFixReturnType(methodSymbolOpt, node, taskType, taskOfTType)
......
......@@ -108,7 +108,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
// actually looking at something Nullable (and not some type that uses a similar
// syntactic pattern).
var semanticModel = context.SemanticModel;
var nullableType = semanticModel.Compilation.GetTypeByMetadataName("System.Nullable`1");
var nullableType = semanticModel.Compilation.GetTypeByMetadataName(typeof(Nullable<>).FullName);
if (nullableType == null)
{
return;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
......@@ -44,7 +45,7 @@ protected override void InitializeWorker(AnalysisContext context)
private void OnCompilationStart(CompilationStartAnalysisContext context)
{
var ienumerableType = context.Compilation.GetTypeByMetadataName("System.Collections.IEnumerable") as INamedTypeSymbol;
var ienumerableType = context.Compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
if (ienumerableType != null)
{
context.RegisterSyntaxNodeAction(
......
......@@ -54,7 +54,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Iterator
End If
' Check that return type of containing method is convertible to IEnumerable
Dim ienumerableSymbol As INamedTypeSymbol = model.Compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerable`1")
Dim ienumerableSymbol = model.Compilation.GetTypeByMetadataName(GetType(IEnumerable(Of)).FullName)
If ienumerableSymbol Is Nothing Then
Return Nothing
End If
......
......@@ -51,7 +51,7 @@ internal static partial class ICodeDefinitionFactoryExtensions
IEnumerable<ISymbol> members,
CancellationToken cancellationToken)
{
var iequatableType = compilation.GetTypeByMetadataName("System.IEquatable`1");
var iequatableType = compilation.GetTypeByMetadataName(typeof(IEquatable<>).FullName);
var statements = ArrayBuilder<SyntaxNode>.GetInstance();
// Come up with a good name for the local variable we're going to compare against.
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace Microsoft.CodeAnalysis.Shared.Extensions
{
......@@ -45,108 +52,69 @@ public static ImmutableArray<IAssemblySymbol> GetReferencedAssemblySymbols(this
}
public static INamedTypeSymbol AttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Attribute");
}
=> compilation.GetTypeByMetadataName(typeof(Attribute).FullName);
public static INamedTypeSymbol ExceptionType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Exception");
}
=> compilation.GetTypeByMetadataName(typeof(Exception).FullName);
public static INamedTypeSymbol DesignerCategoryAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.ComponentModel.DesignerCategoryAttribute");
}
=> compilation.GetTypeByMetadataName("System.ComponentModel.DesignerCategoryAttribute");
public static INamedTypeSymbol DesignerGeneratedAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("Microsoft.VisualBasic.CompilerServices.DesignerGeneratedAttribute");
}
=> compilation.GetTypeByMetadataName("Microsoft.VisualBasic.CompilerServices.DesignerGeneratedAttribute");
public static INamedTypeSymbol HideModuleNameAttribute(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("Microsoft.VisualBasic.HideModuleNameAttribute");
}
=> compilation.GetTypeByMetadataName("Microsoft.VisualBasic.HideModuleNameAttribute");
public static INamedTypeSymbol EventArgsType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.EventArgs");
}
=> compilation.GetTypeByMetadataName(typeof(EventArgs).FullName);
public static INamedTypeSymbol NotImplementedExceptionType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.NotImplementedException");
}
=> compilation.GetTypeByMetadataName(typeof(NotImplementedException).FullName);
public static INamedTypeSymbol EqualityComparerOfTType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Collections.Generic.EqualityComparer`1");
}
=> compilation.GetTypeByMetadataName(typeof(EqualityComparer<>).FullName);
public static INamedTypeSymbol ActionType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Action");
}
=> compilation.GetTypeByMetadataName(typeof(Action).FullName);
public static INamedTypeSymbol ExpressionOfTType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Linq.Expressions.Expression`1");
}
=> compilation.GetTypeByMetadataName("System.Linq.Expressions.Expression`1");
public static INamedTypeSymbol EditorBrowsableAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.ComponentModel.EditorBrowsableAttribute");
}
=> compilation.GetTypeByMetadataName(typeof(EditorBrowsableAttribute).FullName);
public static INamedTypeSymbol EditorBrowsableStateType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.ComponentModel.EditorBrowsableState");
}
=> compilation.GetTypeByMetadataName(typeof(EditorBrowsableState).FullName);
public static INamedTypeSymbol TaskType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
}
=> compilation.GetTypeByMetadataName(typeof(Task).FullName);
public static INamedTypeSymbol TaskOfTType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
}
=> compilation.GetTypeByMetadataName(typeof(Task<>).FullName);
public static INamedTypeSymbol ValueTaskOfTType(this Compilation compilation)
=> compilation.GetTypeByMetadataName("System.Threading.Tasks.ValueTask`1");
public static INamedTypeSymbol IEnumerableOfTType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Collections.Generic.IEnumerable`1");
}
=> compilation.GetTypeByMetadataName(typeof(IEnumerable<>).FullName);
public static INamedTypeSymbol SerializableAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.SerializableAttribute");
}
=> compilation.GetTypeByMetadataName("System.SerializableAttribute");
public static INamedTypeSymbol CoClassType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Runtime.InteropServices.CoClassAttribute");
}
=> compilation.GetTypeByMetadataName(typeof(CoClassAttribute).FullName);
public static INamedTypeSymbol ComAliasNameAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Runtime.InteropServices.ComAliasNameAttribute");
}
=> compilation.GetTypeByMetadataName("System.Runtime.InteropServices.ComAliasNameAttribute");
public static INamedTypeSymbol SuppressMessageAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Diagnostics.CodeAnalysis.SuppressMessageAttribute");
}
=> compilation.GetTypeByMetadataName(typeof(SuppressMessageAttribute).FullName);
public static INamedTypeSymbol TupleElementNamesAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.TupleElementNamesAttribute");
}
=> compilation.GetTypeByMetadataName(typeof(TupleElementNamesAttribute).FullName);
public static INamedTypeSymbol DynamicAttributeType(this Compilation compilation)
{
return compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.DynamicAttribute");
}
}
}
=> compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.DynamicAttribute");
}
}
\ No newline at end of file
......@@ -6,9 +6,6 @@ Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.CodeCleanup
Friend Module AsyncOrIteratorFunctionReturnTypeFixer
Private Const s_system_Threading_Tasks_Task_Name As String = "System.Threading.Tasks.Task"
Private Const s_system_Threading_Tasks_Task_Of_T_Name As String = "System.Threading.Tasks.Task`1"
Private Const s_system_Collections_IEnumerable_Name As String = "System.Collections.IEnumerable"
Public Function RewriteMethodStatement(func As MethodStatementSyntax, semanticModel As SemanticModel, cancellationToken As CancellationToken) As MethodStatementSyntax
Return RewriteMethodStatement(func, semanticModel, oldFunc:=func, cancellationToken:=cancellationToken)
......@@ -85,7 +82,7 @@ Namespace Microsoft.CodeAnalysis.CodeCleanup
Return False
End If
Dim taskType = semanticModel.Compilation.GetTypeByMetadataName(s_system_Threading_Tasks_Task_Name)
Dim taskType = semanticModel.Compilation.GetTypeByMetadataName(GetType(Task).FullName)
If asClauseOpt Is Nothing Then
' Without an AsClause: Async functions are pretty listed to return Task.
If taskType IsNot Nothing AndAlso parameterListOpt IsNot Nothing Then
......@@ -95,7 +92,7 @@ Namespace Microsoft.CodeAnalysis.CodeCleanup
Else
' 2) With an AsClause: If the return type R is a valid non-error type, pretty list as follows:
' (a) Async functions: If R is not Task or Task(Of T) or an instantiation of Task(Of T), pretty list return type to Task(Of R).
Dim taskOfT = semanticModel.Compilation.GetTypeByMetadataName(s_system_Threading_Tasks_Task_Of_T_Name)
Dim taskOfT = semanticModel.Compilation.GetTypeByMetadataName(GetType(Task(Of)).FullName)
Dim returnType = semanticModel.GetTypeInfo(oldAsClauseOpt.Type, cancellationToken).Type
If returnType IsNot Nothing AndAlso Not returnType.IsErrorType() AndAlso
taskType IsNot Nothing AndAlso taskOfT IsNot Nothing AndAlso
......@@ -119,7 +116,7 @@ Namespace Microsoft.CodeAnalysis.CodeCleanup
If asClauseOpt Is Nothing Then
' Without an AsClause: Iterator functions are pretty listed to return IEnumerable.
Dim iEnumerableType = semanticModel.Compilation.GetTypeByMetadataName(s_system_Collections_IEnumerable_Name)
Dim iEnumerableType = semanticModel.Compilation.GetTypeByMetadataName(GetType(IEnumerable).FullName)
If iEnumerableType IsNot Nothing Then
GenerateFunctionAsClause(iEnumerableType, parameterListOpt, asClauseOpt, semanticModel, position)
Return True
......
......@@ -406,8 +406,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim types = InferTypes(awaitExpression, filterUnusable:=False)
Dim task = Me.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task")
Dim taskOfT = Me.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1")
Dim task = Me.Compilation.GetTypeByMetadataName(GetType(Task).FullName)
Dim taskOfT = Me.Compilation.GetTypeByMetadataName(GetType(Task(Of)).FullName)
If task Is Nothing OrElse taskOfT Is Nothing Then
Return SpecializedCollections.EmptyEnumerable(Of TypeInferenceInfo)()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册