-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTestAssemblyLoaderFactory.cs
38 lines (32 loc) · 1.86 KB
/
TestAssemblyLoaderFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.ApiSymbolExtensions;
using Microsoft.DotNet.ApiSymbolExtensions.Logging;
using Microsoft.DotNet.ApiSymbolExtensions.Tests;
namespace Microsoft.DotNet.GenAPI.Tests;
public class TestAssemblyLoaderFactory
{
public static (IAssemblySymbolLoader, Dictionary<string, IAssemblySymbol>) CreateFromTexts(ILog log, (string, string)[] assemblyTexts, IEnumerable<KeyValuePair<string, ReportDiagnostic>>? diagnosticOptions = null, bool respectInternals = false, bool allowUnsafe = false)
{
if (assemblyTexts.Length == 0)
{
return (new AssemblySymbolLoader(log, diagnosticOptions, resolveAssemblyReferences: true, includeInternalSymbols: respectInternals),
new Dictionary<string, IAssemblySymbol>());
}
AssemblySymbolLoader loader = new(log, diagnosticOptions, resolveAssemblyReferences: true, includeInternalSymbols: respectInternals);
loader.AddReferenceSearchPaths(typeof(object).Assembly!.Location!);
loader.AddReferenceSearchPaths(typeof(DynamicAttribute).Assembly!.Location!);
Dictionary<string, IAssemblySymbol> assemblySymbols = new();
foreach ((string assemblyName, string assemblyText) in assemblyTexts)
{
using Stream assemblyStream = SymbolFactory.EmitAssemblyStreamFromSyntax(assemblyText, diagnosticOptions, enableNullable: true, allowUnsafe: allowUnsafe, assemblyName: assemblyName);
if (loader.LoadAssembly(assemblyName, assemblyStream) is IAssemblySymbol assemblySymbol)
{
assemblySymbols.Add(assemblyName, assemblySymbol);
}
}
return (loader, assemblySymbols);
}
}