-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Src gen: use GetBestTypeByMetadataName instead of GetTypeByMetadataName #59092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
28db0c6
25dcab8
ff67850
a3e6eb1
9c89de0
46b3905
051ed1b
4d6c7a3
7a4eee9
8e2ca47
e1ac8d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Reflection; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
|
|
||
| namespace Microsoft.Extensions.Logging.Generators.Tests | ||
| { | ||
| public class CompilationHelper | ||
| { | ||
| public static Compilation CreateCompilation( | ||
| string source, | ||
| MetadataReference[]? additionalReferences = null, | ||
| string assemblyName = "TestAssembly") | ||
| { | ||
| string corelib = Assembly.GetAssembly(typeof(object))!.Location; | ||
| string runtimeDir = Path.GetDirectoryName(corelib)!; | ||
|
|
||
| var refs = new List<MetadataReference>(); | ||
| refs.Add(MetadataReference.CreateFromFile(corelib)); | ||
| refs.Add(MetadataReference.CreateFromFile(Path.Combine(runtimeDir, "netstandard.dll"))); | ||
| refs.Add(MetadataReference.CreateFromFile(Path.Combine(runtimeDir, "System.Runtime.dll"))); | ||
| refs.Add(MetadataReference.CreateFromFile(typeof(ILogger).Assembly.Location)); | ||
| refs.Add(MetadataReference.CreateFromFile(typeof(LoggerMessageAttribute).Assembly.Location)); | ||
|
|
||
| if (additionalReferences != null) | ||
| { | ||
| foreach (MetadataReference reference in additionalReferences) | ||
| { | ||
| refs.Add(reference); | ||
| } | ||
| } | ||
|
|
||
| CSharpParseOptions options = CSharpParseOptions.Default; | ||
|
|
||
| #if ROSLYN4_0_OR_GREATER | ||
| // workaround https://github.com/dotnet/roslyn/pull/55866. We can remove "LangVersion=Preview" when we get a Roslyn build with that change. | ||
| options = options.WithLanguageVersion(LanguageVersion.Preview); | ||
| #endif | ||
|
|
||
| return CSharpCompilation.Create( | ||
| assemblyName, | ||
| syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source, options) }, | ||
| references: refs.ToArray(), | ||
| options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary) | ||
| ); | ||
| } | ||
|
|
||
| public static byte[] CreateAssemblyImage(Compilation compilation) | ||
| { | ||
| MemoryStream ms = new MemoryStream(); | ||
| var emitResult = compilation.Emit(ms); | ||
| if (!emitResult.Success) | ||
| { | ||
| throw new InvalidOperationException(); | ||
| } | ||
| return ms.ToArray(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,18 @@ | |
| <TestTrimming Condition="'$(TestTrimming)' == ''">false</TestTrimming> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup Condition="'$(TargetsMobile)' == 'true' and '$(TargetOS)' != 'Browser'"> | ||
| <!-- Microsoft.CodeAnalysis.* assemblies missing in the virtual file system for Browser and the bundle for the mobile platforms --> | ||
| <ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Logging\tests\Microsoft.Extensions.Logging.Generators.Tests\Microsoft.Extensions.Logging.Generators.Roslyn3.11.Tests.csproj" /> | ||
| <ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Logging\tests\Microsoft.Extensions.Logging.Generators.Tests\Microsoft.Extensions.Logging.Generators.Roslyn4.0.Tests.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup Condition="'$(TargetOS)' == 'Browser' and '$(BuildAOTTestsOnHelix)' == 'true' and '$(RunDisabledWasmTests)' != 'true' and '$(RunAOTCompilation)' == 'true'"> | ||
|
||
| <!-- Exceeds VM resources in CI on compilation: https://github.com/dotnet/runtime/issues/51961 --> | ||
| <ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Logging.Abstractions\tests\Microsoft.Extensions.Logging.Generators.Tests\Microsoft.Extensions.Logging.Generators.Roslyn3.11.Tests.csproj" /> | ||
| <ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Logging.Abstractions\tests\Microsoft.Extensions.Logging.Generators.Tests\Microsoft.Extensions.Logging.Generators.Roslyn4.0.Tests.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <!-- Projects that don't support code coverage measurement. --> | ||
| <ItemGroup Condition="'$(Coverage)' == 'true'"> | ||
| <ProjectExclusions Include="$(CommonTestPath)Common.Tests.csproj" /> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.