-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GetVisibleTypeByMetadataName extensions to Compilation.
- Loading branch information
1 parent
c9835b7
commit 02b30d5
Showing
5 changed files
with
91 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Utilities/Compiler/Extensions/CompilationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Analyzer.Utilities.Extensions | ||
{ | ||
/// <summary> | ||
/// Provides extensions to <see cref="Compilation"/>. | ||
/// </summary> | ||
internal static class CompilationExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the type within the compilation's assembly using its canonical CLR metadata name. If not found, gets the first public type from all referenced assemblies. If not found, gets the first type from all referenced assemblies. | ||
/// </summary> | ||
/// <param name="compilation">The compilation.</param> | ||
/// <param name="fullyQualifiedMetadataName">The fully qualified metadata name.</param> | ||
/// <returns>A <see cref="INamedTypeSymbol"/> if found; <see langword="null"/> otherwise.</returns> | ||
internal static INamedTypeSymbol GetVisibleTypeByMetadataName(this Compilation compilation, string fullyQualifiedMetadataName) | ||
{ | ||
var typeSymbol = compilation.GetTypeByMetadataName(fullyQualifiedMetadataName); | ||
if (typeSymbol is object) | ||
{ | ||
return typeSymbol; | ||
} | ||
|
||
var segments = fullyQualifiedMetadataName.Split('.'); | ||
|
||
var @namespace = compilation.GlobalNamespace; | ||
|
||
for (var s = 0; s < segments.Length - 1; s++) | ||
{ | ||
@namespace = @namespace.GetMembers(segments[s]).OfType<INamespaceSymbol>().SingleOrDefault(); | ||
|
||
if (@namespace is null) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
var metadataName = segments[segments.Length - 1]; | ||
INamedTypeSymbol publicType = null; | ||
INamedTypeSymbol anyType = null; | ||
|
||
foreach (var member in @namespace.GetMembers()) | ||
{ | ||
if (member is INamedTypeSymbol type && type.MetadataName == metadataName) | ||
{ | ||
if (type.ContainingAssembly.Equals(compilation.Assembly)) | ||
{ | ||
return type; | ||
} | ||
else if (publicType is null && type.DeclaredAccessibility == Accessibility.Public) | ||
{ | ||
publicType = type; | ||
break; | ||
} | ||
else if (anyType is null) | ||
{ | ||
anyType = type; | ||
} | ||
} | ||
} | ||
|
||
return publicType ?? anyType; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters