I used "ef dbcontext scaffold" tools based on .NET Core and SQL Server (AdventureWorks) and I generated a source code that is uploaded.
The source code is valid and compiled in ClassLibrary but I need Load and compile it with Roslyn.
ef_source.cs.txt
so I tried the below extension method
public static Type[] ToTypes(this string source, out IEnumerable<string> failures)
{
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
string assemblyName = Path.GetRandomFileName();
var assemblyPath = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location);
List<MetadataReference> references = new List<MetadataReference>();
var referencedAssemblies = Assembly.GetEntryAssembly().GetReferencedAssemblies();
foreach (var referencedAssembly in referencedAssemblies)
{
var loadedAssembly = Assembly.Load(referencedAssembly);
references.Add(MetadataReference.CreateFromFile(loadedAssembly.Location));
}
references.Add(MetadataReference.CreateFromFile(typeof(HashSet<>).GetTypeInfo().Assembly.Location));
references.Add(MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location));
references.Add(MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location));
references.Add(MetadataReference.CreateFromFile(typeof(DbContext).GetTypeInfo().Assembly.Location));
/*
references variable contains these MetadataReference =>
System.Runtime.dll
System.Runtime.InteropServices.dll
System.Reflection.dll
System.IO.dll
System.Text.RegularExpressions.dll
System.ComponentModel.Primitives.dll
DotLiquid.dll
System.Diagnostics.Debug.dll
System.Collections.dll
Microsoft.Extensions.Logging.Abstractions.dll
Microsoft.CodeAnalysis.dll
System.Threading.Tasks.dll
Microsoft.CodeAnalysis.CSharp.dll
Microsoft.CodeAnalysis.Scripting.dll
System.Collections.Immutable.dll
System.Linq.dll
System.Runtime.Extensions.dll
System.Threading.dll
System.Text.Encoding.dll
System.Reflection.Extensions.dll
Microsoft.CodeAnalysis.CSharp.Scripting.dll
System.Runtime.Loader.dll
Microsoft.EntityFrameworkCore.dll
System.Collections.dll
System.Private.CoreLib.ni.dll
System.Linq.dll
Microsoft.EntityFrameworkCore.dll
*/
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
new[] { syntaxTree },
references.ToArray(),
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
var errors = new List<string>();
var diagnostics = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in diagnostics)
errors.Add(string.Format("{0}: {1}", diagnostic.Id, diagnostic.GetMessage()));
failures = errors;
}
else
{
failures = null;
ms.Seek(0, SeekOrigin.Begin);
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(ms.ToArray()));
Type[] types = assembly.GetTypes();
return types;
}
}
return null;
}
and for Compilation and GetTypes
IEnumerable<string> failures;
var types = ef_source.ToTypes(out failures); // ef_source => attachment
I have more that 800 errors !
but most errors are dependent on mscorelib.dll
CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
I used "ef dbcontext scaffold" tools based on .NET Core and SQL Server (AdventureWorks) and I generated a source code that is uploaded.
The source code is valid and compiled in ClassLibrary but I need Load and compile it with Roslyn.
ef_source.cs.txt
so I tried the below extension method
and for Compilation and GetTypes
I have more that 800 errors !
but most errors are dependent on mscorelib.dll