-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better name than MetadataReferences.CreateBinary
- Loading branch information
1 parent
73a55bd
commit d141c96
Showing
3 changed files
with
104 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Gu.Roslyn.Asserts; | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
|
||
/// <summary> | ||
/// Helper for getting meta data reference from code. | ||
/// </summary> | ||
public static class BinaryReference | ||
{ | ||
/// <summary> | ||
/// Create a binary reference from strings. | ||
/// This is useful when testing for example deriving from a base class not in source. | ||
/// </summary> | ||
/// <param name="code">The code to create a dll project from.</param> | ||
/// <returns>A <see cref="MetadataReference"/>.</returns> | ||
public static MetadataReference Compile(params string[] code) | ||
{ | ||
return Compile(code, Settings.Default.WithCompilationOptions(CodeFactory.DllCompilationOptions)); | ||
} | ||
|
||
/// <summary> | ||
/// Create a binary reference from strings. | ||
/// This is useful when testing for example deriving from a base class not in source. | ||
/// </summary> | ||
/// <param name="code">The code to create a dll project from.</param> | ||
/// <param name="settings">The <see cref="Settings"/>.</param> | ||
/// <returns>A <see cref="MetadataReference"/>.</returns> | ||
public static MetadataReference Compile(IEnumerable<string> code, Settings? settings = null) | ||
{ | ||
settings ??= Settings.Default; | ||
var sln = CodeFactory.CreateSolutionWithOneProject( | ||
code, | ||
settings.ParseOptions, | ||
settings.CompilationOptions, | ||
settings.MetadataReferences); | ||
RoslynAssert.NoCompilerDiagnostics(sln); | ||
|
||
using var ms = new MemoryStream(); | ||
_ = sln.Projects.Single().GetCompilationAsync().GetAwaiter().GetResult()!.Emit(ms); | ||
ms.Position = 0; | ||
return MetadataReference.CreateFromStream(ms); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
namespace Gu.Roslyn.Asserts | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
|
||
public static partial class MetadataReferences | ||
{ | ||
/// <summary> | ||
/// Get the meta data references specified with <see cref="MetadataReferenceAttribute"/> and <see cref="MetadataReferencesAttribute"/> in the test assemblies. | ||
/// </summary> | ||
[Obsolete("Use Settings.Default")] | ||
public static ImmutableArray<MetadataReference> FromAttributes() | ||
{ | ||
if (!fromAttributes.IsDefault) | ||
{ | ||
return fromAttributes; | ||
} | ||
|
||
var set = new HashSet<MetadataReference>(MetadataReferenceComparer.Default); | ||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) | ||
{ | ||
var attributes = Attribute.GetCustomAttributes(assembly, typeof(MetadataReferenceAttribute)); | ||
foreach (var single in attributes.Cast<MetadataReferenceAttribute>()) | ||
{ | ||
set.Add(single.MetadataReference); | ||
} | ||
|
||
attributes = Attribute.GetCustomAttributes(assembly, typeof(TransitiveMetadataReferencesAttribute)); | ||
foreach (var transitive in attributes.Cast<TransitiveMetadataReferencesAttribute>()) | ||
{ | ||
set.UnionWith(transitive.MetadataReferences); | ||
} | ||
|
||
if (assembly.GetCustomAttribute<MetadataReferencesAttribute>() is { } attribute) | ||
{ | ||
set.UnionWith(attribute.MetadataReferences); | ||
} | ||
} | ||
|
||
fromAttributes = ImmutableArray.CreateRange(set); | ||
return fromAttributes; | ||
} | ||
|
||
/// <summary> | ||
/// Create a binary reference from strings. | ||
/// This is useful when testing for example deriving from a base class not in source. | ||
/// </summary> | ||
/// <param name="code">The code to create a dll project from.</param> | ||
/// <returns>A <see cref="MetadataReference"/>.</returns> | ||
[Obsolete("Use MetadataReference.Compile()")] | ||
public static MetadataReference CreateBinary(params string[] code) => BinaryReference.Compile(code); | ||
} | ||
} |