diff --git a/Gu.Roslyn.Asserts/MetadataReferences/BinaryReference.cs b/Gu.Roslyn.Asserts/MetadataReferences/BinaryReference.cs new file mode 100644 index 00000000..459258e4 --- /dev/null +++ b/Gu.Roslyn.Asserts/MetadataReferences/BinaryReference.cs @@ -0,0 +1,46 @@ +namespace Gu.Roslyn.Asserts; + +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.CodeAnalysis; + +/// +/// Helper for getting meta data reference from code. +/// +public static class BinaryReference +{ + /// + /// Create a binary reference from strings. + /// This is useful when testing for example deriving from a base class not in source. + /// + /// The code to create a dll project from. + /// A . + public static MetadataReference Compile(params string[] code) + { + return Compile(code, Settings.Default.WithCompilationOptions(CodeFactory.DllCompilationOptions)); + } + + /// + /// Create a binary reference from strings. + /// This is useful when testing for example deriving from a base class not in source. + /// + /// The code to create a dll project from. + /// The . + /// A . + public static MetadataReference Compile(IEnumerable 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); + } +} diff --git a/Gu.Roslyn.Asserts/MetadataReferences/MetadataReferences.cs b/Gu.Roslyn.Asserts/MetadataReferences/MetadataReferences.cs index e1ac8649..0dea58a9 100644 --- a/Gu.Roslyn.Asserts/MetadataReferences/MetadataReferences.cs +++ b/Gu.Roslyn.Asserts/MetadataReferences/MetadataReferences.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; - using System.IO; using System.Linq; using System.Reflection; using Microsoft.CodeAnalysis; @@ -11,46 +10,10 @@ /// /// Helper for getting meta data references from and . /// - public static class MetadataReferences + public static partial class MetadataReferences { private static ImmutableArray fromAttributes; - /// - /// Get the meta data references specified with and in the test assemblies. - /// - [Obsolete("Use Settings.Default")] - public static ImmutableArray FromAttributes() - { - if (!fromAttributes.IsDefault) - { - return fromAttributes; - } - - var set = new HashSet(MetadataReferenceComparer.Default); - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) - { - var attributes = Attribute.GetCustomAttributes(assembly, typeof(MetadataReferenceAttribute)); - foreach (var single in attributes.Cast()) - { - set.Add(single.MetadataReference); - } - - attributes = Attribute.GetCustomAttributes(assembly, typeof(TransitiveMetadataReferencesAttribute)); - foreach (var transitive in attributes.Cast()) - { - set.UnionWith(transitive.MetadataReferences); - } - - if (assembly.GetCustomAttribute() is { } attribute) - { - set.UnionWith(attribute.MetadataReferences); - } - } - - fromAttributes = ImmutableArray.CreateRange(set); - return fromAttributes; - } - /// /// Create a for the . /// Checks reference assemblies first. @@ -83,27 +46,6 @@ public static MetadataReference CreateFromFile(string assemblyFile) return MetadataReference.CreateFromFile(assemblyFile); } - /// - /// Create a binary reference from strings. - /// This is useful when testing for example deriving from a base class not in source. - /// - /// The code to create a dll project from. - /// A . - public static MetadataReference CreateBinary(params string[] code) - { - var sln = CodeFactory.CreateSolutionWithOneProject( - code, - Settings.Default.ParseOptions, - CodeFactory.DllCompilationOptions, - Settings.Default.MetadataReferences); - RoslynAssert.NoCompilerDiagnostics(sln); - - using var ms = new MemoryStream(); - _ = sln.Projects.Single().GetCompilationAsync().GetAwaiter().GetResult()!.Emit(ms); - ms.Position = 0; - return MetadataReference.CreateFromStream(ms); - } - /// /// Get the for and all assemblies referenced by . /// diff --git a/Gu.Roslyn.Asserts/Obsolete/MetadataReferences.Obsolete.cs b/Gu.Roslyn.Asserts/Obsolete/MetadataReferences.Obsolete.cs new file mode 100644 index 00000000..7c46a5e5 --- /dev/null +++ b/Gu.Roslyn.Asserts/Obsolete/MetadataReferences.Obsolete.cs @@ -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 + { + /// + /// Get the meta data references specified with and in the test assemblies. + /// + [Obsolete("Use Settings.Default")] + public static ImmutableArray FromAttributes() + { + if (!fromAttributes.IsDefault) + { + return fromAttributes; + } + + var set = new HashSet(MetadataReferenceComparer.Default); + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + var attributes = Attribute.GetCustomAttributes(assembly, typeof(MetadataReferenceAttribute)); + foreach (var single in attributes.Cast()) + { + set.Add(single.MetadataReference); + } + + attributes = Attribute.GetCustomAttributes(assembly, typeof(TransitiveMetadataReferencesAttribute)); + foreach (var transitive in attributes.Cast()) + { + set.UnionWith(transitive.MetadataReferences); + } + + if (assembly.GetCustomAttribute() is { } attribute) + { + set.UnionWith(attribute.MetadataReferences); + } + } + + fromAttributes = ImmutableArray.CreateRange(set); + return fromAttributes; + } + + /// + /// Create a binary reference from strings. + /// This is useful when testing for example deriving from a base class not in source. + /// + /// The code to create a dll project from. + /// A . + [Obsolete("Use MetadataReference.Compile()")] + public static MetadataReference CreateBinary(params string[] code) => BinaryReference.Compile(code); + } +}